added session operations and cleanup job

This commit is contained in:
Oliver Bryan
2026-01-09 05:31:42 +00:00
parent 7201646b0a
commit 6884cc1c8e
2 changed files with 38 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
export * from "./issues";
export * from "./organisations";
export * from "./projects";
export * from "./sessions";
export * from "./users";

View File

@@ -0,0 +1,37 @@
import { Session } from "@issue/shared";
import { eq, lt } from "drizzle-orm";
import { db } from "../client";
const generateCsrfToken = () => {
const bytes = new Uint8Array(32);
crypto.getRandomValues(bytes);
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
};
const SESSION_DURATION_MS = 7 * 24 * 60 * 60 * 1000; // 7 days in ms
export async function createSession(userId: number) {
const csrfToken = generateCsrfToken();
const expiresAt = new Date(Date.now() + SESSION_DURATION_MS);
const [session] = await db.insert(Session).values({ userId, csrfToken, expiresAt }).returning();
return session;
}
export async function getSession(sessionId: number) {
const [session] = await db.select().from(Session).where(eq(Session.id, sessionId));
return session;
}
export async function deleteSession(sessionId: number) {
await db.delete(Session).where(eq(Session.id, sessionId));
}
export async function deleteUserSessions(userId: number) {
await db.delete(Session).where(eq(Session.userId, userId));
}
export async function cleanupExpiredSessions() {
const result = await db.delete(Session).where(lt(Session.expiresAt, new Date()));
return result.rowCount ?? 0;
}