This commit is contained in:
2026-10-07 12:10:12 +03:00
parent e384bc4872
commit e676717ffb
28 changed files with 722 additions and 236 deletions
+47
View File
@@ -0,0 +1,47 @@
import type { AuthSession } from './auth.models';
const SESSION_KEY = 'gorodar.session';
export function readAuthSession(): AuthSession | null {
const raw = sessionStorage.getItem(SESSION_KEY);
if (!raw) {
return null;
}
const session = parseSession(raw);
const expired = !!session && typeof session.expiresAt === 'number' && session.expiresAt <= Date.now();
if (!session || !isEmail(session.email) || !session.accessToken || expired) {
sessionStorage.removeItem(SESSION_KEY);
return null;
}
return session;
}
export function writeAuthSession(session: AuthSession): void {
sessionStorage.setItem(SESSION_KEY, JSON.stringify(session));
}
export function clearAuthSession(): void {
sessionStorage.removeItem(SESSION_KEY);
}
function parseSession(raw: string): AuthSession | null {
try {
const parsed: unknown = JSON.parse(raw);
if (!parsed || typeof parsed !== 'object') {
return null;
}
return parsed as AuthSession;
} catch {
return null;
}
}
function isEmail(value: string): boolean {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
}