meridian / src/lib/auth/rotation.ts
code
⌘B branch ⌘K
rotation.ts
session.ts
Dashboard.tsx
ln 14, col 38 | typescript | utf-8
1 import { rotateToken, validateSession } from './auth.utils';
2 import { db } from '../../db';
3 import type { Session, RotationResult } from './types';
4
5 // Token rotation window configuration
6 const ROTATION_WINDOW = 5 * 60 * 1000; // 5 minutes
7 const LOCK_TIMEOUT = 5000;
8 const rotationLock = new Map<string, boolean>();
9
10 export async function handleRotation(
11 userId: string,
12 token: string
13 ): Promise<RotationResult> {
14 const session = await validateSession(userId, token);
15
16 if (!session) {
17 throw new Error('INVALID_SESSION');
18 }
19
20 // Acquire rotation lock to prevent race conditions
21 if (rotationLock.has(session.id)) {
22 return { status: 'locked', token: session.token };
23 }
24
25 rotationLock.set(session.id, true);
26
27 try {
28 const { newToken, expiresAt } = await rotateToken(session);
29
30 const updated = await db.sessions.update({
31 where: { id: session.id },
32 data: {
33 token: newToken,
34 expiresAt,
35 lastRotated: new Date(),
36 },
37 });
38
39 return {
40 status: 'rotated',
41 token: updated.token,
42 expiresAt: updated.expiresAt,
43 };
44 } finally {
45 setTimeout(() => {
46 rotationLock.delete(session.id);
47 }, LOCK_TIMEOUT);
48 }
49 }
50
terminal output problems debug
~/meridian $
npx tsc --noEmit --watch
[12:44:01] Starting compilation in watch mode...
[12:44:03] Found 0 errors. Watching for file changes.
[12:47:18] File change detected. Starting incremental compilation...
[12:47:19] ✓ src/lib/auth/rotation.ts compiled successfully
[12:47:19] ✓ 0 errors, 0 warnings
[12:47:20] Running tests...
PASS auth/rotation.test.ts (12 tests, 0.84s)
WARN auth/session.test.ts deprecated: use validateSession() instead
PASS auth/session.test.ts (8 tests, 0.41s)
Test Suites: 2 passed, 2 total
Tests: 20 passed, 20 total
Time: 1.25s
~/meridian $ ▌