File size: 836 Bytes
9a92a42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { getRedisClient } from '../util/redis.util';

export async function createSession(userId: string, token: string, ttl: number = 7 * 24 * 60 * 60) {
  const redis = await getRedisClient();
  const sessionKey = `session:${userId}:${token.substring(0, 10)}`;

  await redis.setEx(sessionKey, ttl, JSON.stringify({
    userId,
    createdAt: new Date().toISOString()
  }));
}

export async function destroySession(userId: string, token: string) {
  const redis = await getRedisClient();
  const sessionKey = `session:${userId}:${token.substring(0, 10)}`;
  await redis.del(sessionKey);
}

export async function destroyAllUserSessions(userId: string) {
  const redis = await getRedisClient();
  const pattern = `session:${userId}:*`;

  const keys = await redis.keys(pattern);
  if (keys.length > 0) {
    await redis.del(keys);
  }
}