Prashikshak / API /src /util /session.util.ts
Abhisingh-18's picture
Initial commit: Prashikshak - disaster management training platform
9a92a42
Raw
History Blame Contribute Delete
836 Bytes
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);
}
}