33 lines
800 B
TypeScript
33 lines
800 B
TypeScript
export type AuthStatus = 'authenticated' | 'guest' | null;
|
|
|
|
const KEY = 'han_auth_status';
|
|
|
|
function safeGet(key: string): string | null {
|
|
try { return localStorage.getItem(key); } catch { return null; }
|
|
}
|
|
|
|
function safeSet(key: string, value: string) {
|
|
try { localStorage.setItem(key, value); } catch { /* ignore */ }
|
|
}
|
|
|
|
function safeRemove(key: string) {
|
|
try { localStorage.removeItem(key); } catch { /* ignore */ }
|
|
}
|
|
|
|
export function getAuthStatus(): AuthStatus {
|
|
return (safeGet(KEY) as AuthStatus) ?? null;
|
|
}
|
|
|
|
export function setAuthStatus(status: AuthStatus) {
|
|
if (status === null) safeRemove(KEY);
|
|
else safeSet(KEY, status);
|
|
}
|
|
|
|
export function isGuest() {
|
|
return getAuthStatus() === 'guest';
|
|
}
|
|
|
|
export function isAuthenticated() {
|
|
return getAuthStatus() === 'authenticated';
|
|
}
|