From ad9ed65170a41d2b53dbe225e6e8b10454682d52 Mon Sep 17 00:00:00 2001 From: mohammadkhizerkhan Date: Mon, 4 Sep 2023 23:26:55 +0530 Subject: [PATCH] added encrypted storage methods --- package.json | 1 + src/screens/AuthScreen/AuthScreen.tsx | 3 +- src/utils/dataStore.tsx | 47 +++++++++++++++++++++++++++ src/utils/index.ts | 1 + yarn.lock | 5 +++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/utils/index.ts diff --git a/package.json b/package.json index 76d9df9f..1fee7475 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "react-native-datepicker": "^1.7.2", "react-native-device-info": "^10.8.0", "react-native-element-dropdown": "^2.0.0", + "react-native-encrypted-storage": "^4.0.3", "react-native-floating-action": "^1.22.0", "react-native-gesture-handler": "^1.10.3", "react-native-image-picker": "^4.7.1", diff --git a/src/screens/AuthScreen/AuthScreen.tsx b/src/screens/AuthScreen/AuthScreen.tsx index a36640b2..9eaa1ede 100644 --- a/src/screens/AuthScreen/AuthScreen.tsx +++ b/src/screens/AuthScreen/AuthScreen.tsx @@ -15,7 +15,7 @@ import { AuthScreenButton } from './Button'; import { Toast } from 'react-native-toast-message/lib/src/Toast'; import { AuthContext } from '../../context/AuthContext'; import { getUserData } from './Util'; -import { storeData } from '../../utils/dataStore'; +import { Storage, storeData } from '../../utils/dataStore'; import { SafeAreaView } from 'react-native-safe-area-context'; import { ActivityIndicator } from 'react-native'; import Images from '../../constants/images/Image'; @@ -258,7 +258,6 @@ const AuthScreen = () => { onPress={activateCamera} /> - {cameraActive && ( { try { @@ -16,3 +18,48 @@ export const getData = async (item: string) => { }) .catch((err) => console.log(err)); }; + +export enum StorageKey { + TOKEN = 'TOKEN', +} + +export const Storage = { + async setItem(key: string, value: string): Promise { + await EncryptedStorage.setItem(key, value.toString()); + }, + + async getItem(key: string): Promise { + return EncryptedStorage.getItem(key); + }, + + async deleteItem(key: string): Promise { + try { + await EncryptedStorage.removeItem(key); + } catch (error: any) { + // There was an error on the native side + // You can find out more about this error by using the `error.code` property + console.log(error.code); // ex: -25300 (errSecItemNotFound) + Toast.show({ + type: 'error', + text1: error.code, + position: 'bottom', + bottomOffset: 80, + }); + } + }, + + async clearStorage(): Promise { + try { + await EncryptedStorage.clear(); + // Congrats! You've just cleared the device storage! + } catch (error: any) { + // There was an error on the native side + Toast.show({ + type: 'error', + text1: error.code, + position: 'bottom', + bottomOffset: 80, + }); + } + }, +}; diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 00000000..78014747 --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1 @@ +export * from './dataStore' \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index c34eda89..f883e9b2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6238,6 +6238,11 @@ react-native-element-dropdown@^2.0.0: dependencies: lodash "^4.17.21" +react-native-encrypted-storage@^4.0.3: + version "4.0.3" + resolved "https://registry.yarnpkg.com/react-native-encrypted-storage/-/react-native-encrypted-storage-4.0.3.tgz#2a4d65459870511e8f4ccd22f02433dab7fa5e91" + integrity sha512-0pJA4Aj2S1PIJEbU7pN/Qvf7JIJx3hFywx+i+bLHtgK0/6Zryf1V2xVsWcrD50dfiu3jY1eN2gesQ5osGxE7jA== + react-native-floating-action@^1.22.0: version "1.22.0" resolved "https://registry.yarnpkg.com/react-native-floating-action/-/react-native-floating-action-1.22.0.tgz#7ec476f320e16855ad4077a3a9f36511d1718a52"