| 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); | |
| } | |
| } |