30 lines
752 B
TypeScript
30 lines
752 B
TypeScript
import AsyncStorage from '@react-native-async-storage/async-storage'
|
|
import type { PbUserRecord } from './pocketbase'
|
|
|
|
const KEY = 'pb_auth_session_v1'
|
|
|
|
export type StoredPbSession = {
|
|
token: string
|
|
record: PbUserRecord
|
|
}
|
|
|
|
export async function loadPbSession(): Promise<StoredPbSession | null> {
|
|
try {
|
|
const raw = await AsyncStorage.getItem(KEY)
|
|
if (!raw) return null
|
|
const j = JSON.parse(raw) as StoredPbSession
|
|
if (!j?.token || !j?.record?.id) return null
|
|
return j
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export async function savePbSession(s: StoredPbSession): Promise<void> {
|
|
await AsyncStorage.setItem(KEY, JSON.stringify(s))
|
|
}
|
|
|
|
export async function clearPbSession(): Promise<void> {
|
|
await AsyncStorage.removeItem(KEY)
|
|
}
|