fix and update
This commit is contained in:
35
apps/frontend/lib/pin-idle-storage.ts
Normal file
35
apps/frontend/lib/pin-idle-storage.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
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);
|
||||
}
|
||||
10
apps/frontend/lib/pin-input.ts
Normal file
10
apps/frontend/lib/pin-input.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export const PIN_MIN_LENGTH = 4;
|
||||
export const PIN_MAX_LENGTH = 6;
|
||||
|
||||
export function sanitizePinInput(value: string, maxLength: number = PIN_MAX_LENGTH): string {
|
||||
return value.replace(/\D/g, '').slice(0, maxLength);
|
||||
}
|
||||
|
||||
export function isPinInputComplete(value: string, minLength: number = PIN_MIN_LENGTH): boolean {
|
||||
return /^\d+$/.test(value) && value.length >= minLength && value.length <= PIN_MAX_LENGTH;
|
||||
}
|
||||
Reference in New Issue
Block a user