summaryrefslogtreecommitdiff
path: root/app/utils/keychain.ts
diff options
context:
space:
mode:
authorThomas F. K. Jorna <[email protected]>2021-07-14 15:10:31 +0200
committerThomas F. K. Jorna <[email protected]>2021-07-14 15:10:31 +0200
commite5021187e96b78b53203bd95d08d6818aea47d17 (patch)
tree37ec45d00eb963db53cd4bb4f04a770414b351cc /app/utils/keychain.ts
New Ignite 7.0.6 app
Diffstat (limited to 'app/utils/keychain.ts')
-rw-r--r--app/utils/keychain.ts63
1 files changed, 63 insertions, 0 deletions
diff --git a/app/utils/keychain.ts b/app/utils/keychain.ts
new file mode 100644
index 0000000..34774bb
--- /dev/null
+++ b/app/utils/keychain.ts
@@ -0,0 +1,63 @@
+import * as ReactNativeKeychain from "react-native-keychain"
+
+/**
+ * Saves some credentials securely.
+ *
+ * @param username The username
+ * @param password The password
+ * @param server The server these creds are for.
+ */
+export async function save(username: string, password: string, server?: string) {
+ if (server) {
+ await ReactNativeKeychain.setInternetCredentials(server, username, password)
+ return true
+ } else {
+ return ReactNativeKeychain.setGenericPassword(username, password)
+ }
+}
+
+/**
+ * Loads credentials that were already saved.
+ *
+ * @param server The server that these creds are for
+ */
+export async function load(server?: string) {
+ if (server) {
+ const creds = await ReactNativeKeychain.getInternetCredentials(server)
+ return {
+ username: creds ? creds.username : null,
+ password: creds ? creds.password : null,
+ server,
+ }
+ } else {
+ const creds = await ReactNativeKeychain.getGenericPassword()
+ if (typeof creds === "object") {
+ return {
+ username: creds.username,
+ password: creds.password,
+ server: null,
+ }
+ } else {
+ return {
+ username: null,
+ password: null,
+ server: null,
+ }
+ }
+ }
+}
+
+/**
+ * Resets any existing credentials for the given server.
+ *
+ * @param server The server which has these creds
+ */
+export async function reset(server?: string) {
+ if (server) {
+ await ReactNativeKeychain.resetInternetCredentials(server)
+ return true
+ } else {
+ const result = await ReactNativeKeychain.resetGenericPassword()
+ return result
+ }
+}