diff options
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); +}; |
