aboutsummaryrefslogtreecommitdiffstats
path: root/backend/src/utils/session.ts
blob: cc1facda102d7551094ff11b21247c0e4d2457a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);
};
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage