Skip to content

Commit

Permalink
added encrypted storage methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadkhizerkhan committed Sep 4, 2023
1 parent efdba8a commit ad9ed65
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions src/screens/AuthScreen/AuthScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check failure on line 18 in src/screens/AuthScreen/AuthScreen.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

'Storage' is defined but never used
import { SafeAreaView } from 'react-native-safe-area-context';
import { ActivityIndicator } from 'react-native';
import Images from '../../constants/images/Image';
Expand Down Expand Up @@ -258,7 +258,6 @@ const AuthScreen = () => {
onPress={activateCamera}
/>
</View>

{cameraActive && (
<CameraScreen
style={StyleSheet.absoluteFill}
Expand Down
47 changes: 47 additions & 0 deletions src/utils/dataStore.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import EncryptedStorage from 'react-native-encrypted-storage';
import { Toast } from 'react-native-toast-message/lib/src/Toast';

export const storeData = async (name: string, value: string) => {
try {
Expand All @@ -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<void> {
await EncryptedStorage.setItem(key, value.toString());
},

async getItem(key: string): Promise<string | null> {
return EncryptedStorage.getItem(key);
},

async deleteItem(key: string): Promise<void> {
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<void> {
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,
});
}
},
};
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dataStore'
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ad9ed65

Please sign in to comment.