From eda5691cfcb3be0bb6ccf1b2ad4fa92801ad86c4 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Sun, 29 Jun 2025 19:55:51 -0700 Subject: seperate routes and middleware into seperate files --- backend/src/utils/session.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 backend/src/utils/session.ts (limited to 'backend/src/utils/session.ts') diff --git a/backend/src/utils/session.ts b/backend/src/utils/session.ts new file mode 100644 index 0000000..cc1facd --- /dev/null +++ b/backend/src/utils/session.ts @@ -0,0 +1,35 @@ +import crypto from 'crypto'; +import { prisma } from '../config/db'; + +export const createSession = async (userId: number): Promise => { + const sessionId = crypto.randomUUID(); + const expiresAt = new Date(Date.now() + 24 * 60 * 60 * 1000); // 24 hours + + await prisma.session.create({ + data: { + id: sessionId, + userId, + expiresAt + } + }); + + return sessionId; +}; + +export const cleanupExpiredSessions = async () => { + try { + await prisma.session.deleteMany({ + where: { + expiresAt: { + lt: new Date() + } + } + }); + } catch (error) { + console.error('Session cleanup error:', error); + } +}; + +export const startSessionCleanup = () => { + setInterval(cleanupExpiredSessions, 60 * 60 * 1000); +}; -- cgit v1.2.3