forked from TrustUp-app/TrustUp-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-storage.ts
More file actions
58 lines (50 loc) · 1.99 KB
/
Copy pathauth-storage.ts
File metadata and controls
58 lines (50 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { Platform } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as SecureStore from 'expo-secure-store';
/**
* Secure storage helpers for the authentication JWTs.
*
* - Native (iOS/Android): `expo-secure-store` (Keychain / Keystore)
* - Web: AsyncStorage fallback — SecureStore is not available in browsers
*
* Two separate keys per the auth API contract: an access token (short-lived,
* sent on every request) and a refresh token (longer-lived, used to mint a
* new access token — refresh flow itself is not implemented yet, storage
* only, see #60 follow-up).
*/
const ACCESS_TOKEN_KEY = 'trustup_access_token';
const REFRESH_TOKEN_KEY = 'trustup_refresh_token';
const isWeb = Platform.OS === 'web';
async function getItem(key: string): Promise<string | null> {
try {
return isWeb ? await AsyncStorage.getItem(key) : await SecureStore.getItemAsync(key);
} catch {
return null;
}
}
async function setItem(key: string, value: string): Promise<void> {
if (isWeb) {
await AsyncStorage.setItem(key, value);
return;
}
await SecureStore.setItemAsync(key, value);
}
async function removeItem(key: string): Promise<void> {
if (isWeb) {
await AsyncStorage.removeItem(key);
return;
}
await SecureStore.deleteItemAsync(key);
}
export const getAccessToken = (): Promise<string | null> => getItem(ACCESS_TOKEN_KEY);
export const getRefreshToken = (): Promise<string | null> => getItem(REFRESH_TOKEN_KEY);
/** Persists both tokens after a successful login/register. Refresh token is optional. */
export const setTokens = async (accessToken: string, refreshToken?: string): Promise<void> => {
await setItem(ACCESS_TOKEN_KEY, accessToken);
if (refreshToken) await setItem(REFRESH_TOKEN_KEY, refreshToken);
};
/** Removes both tokens — used on sign out and on a 401 from any API call. */
export const clearTokens = async (): Promise<void> => {
await removeItem(ACCESS_TOKEN_KEY);
await removeItem(REFRESH_TOKEN_KEY);
};