🪻 distributed transcription service thistle.dunkirk.sh

chore: limit user sessions to 10

dunkirk.sh 2aabf30d b92b7cf9

verified
Changed files
+56
src
+34
src/lib/auth.test.ts
···
};
expect(typeof result.count).toBe("number");
});
+
+
test("enforces maximum session limit per user", () => {
+
const userId = 999;
+
+
// Clean up any existing sessions for this user
+
db.run("DELETE FROM sessions WHERE user_id = ?", [userId]);
+
+
// Create 11 sessions (limit is 10)
+
const sessionIds: string[] = [];
+
for (let i = 0; i < 11; i++) {
+
const sessionId = createSession(userId, `192.168.1.${i}`, `Agent ${i}`);
+
sessionIds.push(sessionId);
+
}
+
+
// Count total sessions for user
+
const sessionCount = db
+
.query<{ count: number }, [number]>(
+
"SELECT COUNT(*) as count FROM sessions WHERE user_id = ?",
+
)
+
.get(userId);
+
+
expect(sessionCount?.count).toBe(10);
+
+
// First session should be deleted (oldest)
+
const firstSession = getSession(sessionIds[0]);
+
expect(firstSession).toBeNull();
+
+
// Last session should exist (newest)
+
const lastSession = getSession(sessionIds[10]);
+
expect(lastSession).not.toBeNull();
+
+
// Cleanup
+
db.run("DELETE FROM sessions WHERE user_id = ?", [userId]);
+
});
+22
src/lib/auth.ts
···
import db from "../db/schema";
const SESSION_DURATION = 7 * 24 * 60 * 60; // 7 days in seconds
+
const MAX_SESSIONS_PER_USER = 10; // Maximum number of sessions per user
export type UserRole = "user" | "admin";
···
): string {
const sessionId = crypto.randomUUID();
const expiresAt = Math.floor(Date.now() / 1000) + SESSION_DURATION;
+
+
// Check current session count for user
+
const sessionCount = db
+
.query<{ count: number }, [number]>(
+
"SELECT COUNT(*) as count FROM sessions WHERE user_id = ?",
+
)
+
.get(userId);
+
+
// If at or over limit, delete oldest session(s)
+
if (sessionCount && sessionCount.count >= MAX_SESSIONS_PER_USER) {
+
const sessionsToDelete = sessionCount.count - MAX_SESSIONS_PER_USER + 1;
+
db.run(
+
`DELETE FROM sessions WHERE id IN (
+
SELECT id FROM sessions
+
WHERE user_id = ?
+
ORDER BY created_at ASC
+
LIMIT ?
+
)`,
+
[userId, sessionsToDelete],
+
);
+
}
db.run(
"INSERT INTO sessions (id, user_id, ip_address, user_agent, expires_at) VALUES (?, ?, ?, ?, ?)",