diff options
| author | Pinapelz <yukais@pinapelz.com> | 2025-06-29 19:55:51 -0700 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2025-06-29 22:06:39 -0700 |
| commit | eda5691cfcb3be0bb6ccf1b2ad4fa92801ad86c4 (patch) | |
| tree | ee83300ec2d7fc2763ba6b7887d61c2af0208c7a /backend/src/utils | |
| parent | 1b66788e84c1d2eef875534cd02685b56d08547f (diff) | |
seperate routes and middleware into seperate files
Diffstat (limited to 'backend/src/utils')
| -rw-r--r-- | backend/src/utils/session.ts | 35 |
1 files changed, 35 insertions, 0 deletions
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<string> => { + 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); +}; |
