File tree Expand file tree Collapse file tree 4 files changed +275
-351
lines changed Expand file tree Collapse file tree 4 files changed +275
-351
lines changed Original file line number Diff line number Diff line change 154154 ]
155155 },
156156 "dependencies" : {
157+ "@react-native-async-storage/async-storage" : " ^1.21.0" ,
157158 "expo-secure-store" : " ^12.3.1"
158159 }
159160}
Original file line number Diff line number Diff line change 1+ import * as SecureStore from 'expo-secure-store' ;
2+
3+ class TokenStorage {
4+ private static readonly TOKEN_KEY = 'authToken' ;
5+
6+ static async saveToken ( token : string ) : Promise < void > {
7+ try {
8+ await SecureStore . setItemAsync ( TokenStorage . TOKEN_KEY , token ) ;
9+ } catch ( error ) {
10+ console . error ( 'Error while saving the encrypted token:' , error ) ;
11+ throw error ;
12+ }
13+ }
14+
15+ static async getToken ( ) : Promise < string | null > {
16+ try {
17+ const encryptedToken = await SecureStore . getItemAsync (
18+ TokenStorage . TOKEN_KEY
19+ ) ;
20+ return encryptedToken || null ;
21+ } catch ( error ) {
22+ console . error ( 'Error while retrieving the encrypted token:' , error ) ;
23+ throw error ;
24+ }
25+ }
26+
27+ static async removeToken ( ) : Promise < void > {
28+ try {
29+ await SecureStore . deleteItemAsync ( TokenStorage . TOKEN_KEY ) ;
30+ } catch ( error ) {
31+ console . error ( 'Error while removing the encrypted token:' , error ) ;
32+ throw error ;
33+ }
34+ }
35+ }
36+
37+ export default TokenStorage ;
Original file line number Diff line number Diff line change 1- import * as SecureStore from 'expo-secure-store ' ;
1+ import AsyncStorage from '@react-native-async-storage/async-storage ' ;
22
33class TokenStorage {
44 private static readonly TOKEN_KEY = 'authToken' ;
55
66 static async saveToken ( token : string ) : Promise < void > {
77 try {
8- await SecureStore . setItemAsync ( TokenStorage . TOKEN_KEY , token ) ;
8+ await AsyncStorage . setItem ( TokenStorage . TOKEN_KEY , token ) ;
99 } catch ( error ) {
1010 console . error ( 'Error while saving the encrypted token:' , error ) ;
1111 throw error ;
@@ -14,10 +14,8 @@ class TokenStorage {
1414
1515 static async getToken ( ) : Promise < string | null > {
1616 try {
17- const encryptedToken = await SecureStore . getItemAsync (
18- TokenStorage . TOKEN_KEY
19- ) ;
20- return encryptedToken || null ;
17+ const localToken = await AsyncStorage . getItem ( TokenStorage . TOKEN_KEY ) ;
18+ return localToken || null ;
2119 } catch ( error ) {
2220 console . error ( 'Error while retrieving the encrypted token:' , error ) ;
2321 throw error ;
@@ -26,7 +24,7 @@ class TokenStorage {
2624
2725 static async removeToken ( ) : Promise < void > {
2826 try {
29- await SecureStore . deleteItemAsync ( TokenStorage . TOKEN_KEY ) ;
27+ await AsyncStorage . removeItem ( TokenStorage . TOKEN_KEY ) ;
3028 } catch ( error ) {
3129 console . error ( 'Error while removing the encrypted token:' , error ) ;
3230 throw error ;
You can’t perform that action at this time.
0 commit comments