freshyo/apps/admin-ui/hooks/useJWT.ts
2026-10-16 20:18:56 +05:30

45 lines
1.1 KiB
TypeScript
Executable file

import { StorageService } from 'common-ui'
export const JWT_KEY = 'jwt_token'
export const STAFF_KEY = 'staff_session'
/**
* The staff identity returned by `admin.staffUser.login`, persisted so the drawer
* can still show who is signed in after an app reload (the JWT itself only
* carries the staff name, and the region name is fetched from the API).
*/
export interface StoredStaff {
id: number
name: string
regionId?: number | null
}
export async function saveJWT(token: string) {
await StorageService.setItem(JWT_KEY, token)
}
export async function getJWT() {
return await StorageService.getItem(JWT_KEY)
}
export async function deleteJWT() {
await StorageService.removeItem(JWT_KEY)
}
export async function saveStaff(staff: StoredStaff) {
await StorageService.setItem(STAFF_KEY, JSON.stringify(staff))
}
export async function getStaff(): Promise<StoredStaff | null> {
const raw = await StorageService.getItem(STAFF_KEY)
if (!raw) return null
try {
return JSON.parse(raw) as StoredStaff
} catch {
return null
}
}
export async function deleteStaff() {
await StorageService.removeItem(STAFF_KEY)
}