diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-08 17:06:51 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-08 17:06:51 +0200 |
| commit | bd2f982c489b15ce1165c051fdedf35af979f784 (patch) | |
| tree | 5c76d07f94b34f6b7b31be6a133793b2cdaccd79 /src/lib/server/auth.ts | |
| parent | 6d69c7b1e6a8051338e052cc9d1836f4f1a20f51 (diff) | |
Migrate date handling from Date to Temporal API
Routine scheduling, health series, service stamps, session expiry, and
date formatting move to native Temporal (Instant / ZonedDateTime in
UTC), with ISO strings kept at DB and chart boundaries. Ambient types
in src/temporal-ambient.d.ts cover the native runtime; no polyfill.
Observable behavior and stored formats are unchanged.
Diffstat (limited to 'src/lib/server/auth.ts')
| -rw-r--r-- | src/lib/server/auth.ts | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/src/lib/server/auth.ts b/src/lib/server/auth.ts index f80eda0..3db93f0 100644 --- a/src/lib/server/auth.ts +++ b/src/lib/server/auth.ts @@ -30,15 +30,19 @@ export function verifyPassword(candidate: string): boolean { return timingSafeEqual(actual, expected); } -export function createSession(now = Date.now()): string { - const expiresAt = now + SESSION_MAX_AGE_MS; +export function createSession( + now: Temporal.Instant = Temporal.Now.instant(), +): string { + const expiresAt = now.add({ + milliseconds: SESSION_MAX_AGE_MS, + }).epochMilliseconds; const payload = String(expiresAt); return `${payload}.${sign(payload)}`; } export function isValidSession( value: string | undefined, - now = Date.now(), + now: Temporal.Instant = Temporal.Now.instant(), ): boolean { if (!value) return false; @@ -46,7 +50,8 @@ export function isValidSession( if (!expiresAtText || !signature || extra) return false; const expiresAt = Number(expiresAtText); - if (!Number.isSafeInteger(expiresAt) || expiresAt <= now) return false; + if (!Number.isSafeInteger(expiresAt) || expiresAt <= now.epochMilliseconds) + return false; const expected = Buffer.from(sign(expiresAtText), 'base64url'); const actual = Buffer.from(signature, 'base64url'); @@ -58,7 +63,7 @@ export function isValidSession( export function renewSession( cookies: Cookies, secure: boolean, - now = Date.now(), + now: Temporal.Instant = Temporal.Now.instant(), ): boolean { const current = cookies.get(AUTH_COOKIE_NAME); if (!isValidSession(current, now)) return false; @@ -76,7 +81,7 @@ export function renewSession( export function setSession( cookies: Cookies, secure: boolean, - now = Date.now(), + now: Temporal.Instant = Temporal.Now.instant(), ): void { cookies.set(AUTH_COOKIE_NAME, createSession(now), { path: '/', |
