Blame · Line-by-line history
auth.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| c53cf00 | 1 | import * as SecureStore from 'expo-secure-store'; |
| 2 | import { api, type User } from '../api/client'; | |
| 3 | ||
| 4 | const TOKEN_KEY = 'gluecron_token'; | |
| 5 | const USER_KEY = 'gluecron_user'; | |
| 6 | const HOST_KEY = 'gluecron_host'; | |
| 7 | ||
| 8 | export async function saveToken(token: string): Promise<void> { | |
| 9 | await SecureStore.setItemAsync(TOKEN_KEY, token); | |
| 10 | api.setToken(token); | |
| 11 | } | |
| 12 | ||
| 13 | export async function loadToken(): Promise<string | null> { | |
| 14 | const token = await SecureStore.getItemAsync(TOKEN_KEY); | |
| 15 | if (token) { | |
| 16 | api.setToken(token); | |
| 17 | } | |
| 18 | return token; | |
| 19 | } | |
| 20 | ||
| 21 | export async function clearToken(): Promise<void> { | |
| 22 | await SecureStore.deleteItemAsync(TOKEN_KEY); | |
| 23 | api.clearToken(); | |
| 24 | } | |
| 25 | ||
| 26 | export async function saveUser(user: User): Promise<void> { | |
| 27 | await SecureStore.setItemAsync(USER_KEY, JSON.stringify(user)); | |
| 28 | } | |
| 29 | ||
| 30 | export async function loadUser(): Promise<User | null> { | |
| 31 | const raw = await SecureStore.getItemAsync(USER_KEY); | |
| 32 | if (!raw) return null; | |
| 33 | try { | |
| 34 | return JSON.parse(raw) as User; | |
| 35 | } catch { | |
| 36 | return null; | |
| 37 | } | |
| 38 | } | |
| 39 | ||
| 40 | export async function clearUser(): Promise<void> { | |
| 41 | await SecureStore.deleteItemAsync(USER_KEY); | |
| 42 | } | |
| 43 | ||
| 44 | export async function saveHost(host: string): Promise<void> { | |
| 45 | await SecureStore.setItemAsync(HOST_KEY, host); | |
| 46 | } | |
| 47 | ||
| 48 | export async function loadHost(): Promise<string | null> { | |
| 49 | return SecureStore.getItemAsync(HOST_KEY); | |
| 50 | } | |
| 51 | ||
| 52 | export async function clearAll(): Promise<void> { | |
| 53 | await Promise.all([ | |
| 54 | SecureStore.deleteItemAsync(TOKEN_KEY), | |
| 55 | SecureStore.deleteItemAsync(USER_KEY), | |
| 56 | ]); | |
| 57 | api.clearToken(); | |
| 58 | } |