36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { AUTH_USER_CACHE_KEY } from '@/lib/api';
|
|
|
|
const PIN_IDLE_WATCH_KEY = 'auth:pin-idle-watch-user';
|
|
|
|
function readCachedUserId(): string | null {
|
|
if (typeof window === 'undefined') return null;
|
|
const raw = window.localStorage.getItem(AUTH_USER_CACHE_KEY);
|
|
if (!raw) return null;
|
|
try {
|
|
const parsed = JSON.parse(raw) as { id?: string };
|
|
return parsed.id ?? null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function markPinIdleWatch(userId?: string | null) {
|
|
if (typeof window === 'undefined') return;
|
|
const resolvedUserId = userId ?? readCachedUserId();
|
|
if (!resolvedUserId) return;
|
|
window.localStorage.setItem(PIN_IDLE_WATCH_KEY, resolvedUserId);
|
|
}
|
|
|
|
export function clearPinIdleWatch() {
|
|
if (typeof window === 'undefined') return;
|
|
window.localStorage.removeItem(PIN_IDLE_WATCH_KEY);
|
|
}
|
|
|
|
export function isPinIdleWatchActive(userId?: string | null) {
|
|
if (typeof window === 'undefined') return false;
|
|
const stored = window.localStorage.getItem(PIN_IDLE_WATCH_KEY);
|
|
if (!stored) return false;
|
|
const resolvedUserId = userId ?? readCachedUserId();
|
|
return Boolean(resolvedUserId && stored === resolvedUserId);
|
|
}
|