All files / src/services sessionStorage.ts

43.75% Statements 7/16
0% Branches 0/9
66.66% Functions 2/3
46.66% Lines 7/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 3640x     40x   40x                                             40x 8x     40x 3x    
import * as SecureStore from 'expo-secure-store';
import type { StoredAuthSession } from '@/models/auth';
 
const AUTH_SESSION_KEY = 'auth_session';
 
export async function readStoredSession(): Promise<StoredAuthSession | null> {
  const raw = await SecureStore.getItemAsync(AUTH_SESSION_KEY);
  if (!raw) return null;
 
  try {
    const parsed = JSON.parse(raw) as StoredAuthSession;
    if (
      parsed &&
      typeof parsed.access_token === 'string' &&
      typeof parsed.refresh_token === 'string' &&
      parsed.user &&
      typeof parsed.user.id === 'string'
    ) {
      return parsed;
    }
  } catch {
    // Corrupted session blobs should not block app startup.
  }
 
  await SecureStore.deleteItemAsync(AUTH_SESSION_KEY);
  return null;
}
 
export function writeStoredSession(session: StoredAuthSession): Promise<void> {
  return SecureStore.setItemAsync(AUTH_SESSION_KEY, JSON.stringify(session));
}
 
export function clearStoredSession(): Promise<void> {
  return SecureStore.deleteItemAsync(AUTH_SESSION_KEY);
}