You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
919 B
37 lines
919 B
import { getRedis } from "./redis";
|
|
import { sessionTtlSeconds } from "./session-ttl";
|
|
|
|
export type SessionPayload = {
|
|
userId: number;
|
|
sessionVersion: number;
|
|
createdAt: string;
|
|
};
|
|
|
|
const sessionKey = (id: string) => `sess:${id}`;
|
|
|
|
export async function createSession(
|
|
sessionId: string,
|
|
payload: SessionPayload,
|
|
): Promise<void> {
|
|
const redis = getRedis();
|
|
const ttl = sessionTtlSeconds();
|
|
await redis.set(sessionKey(sessionId), JSON.stringify(payload), "EX", ttl);
|
|
}
|
|
|
|
export async function readSession(
|
|
sessionId: string,
|
|
): Promise<SessionPayload | null> {
|
|
const redis = getRedis();
|
|
const raw = await redis.get(sessionKey(sessionId));
|
|
if (!raw) return null;
|
|
try {
|
|
return JSON.parse(raw) as SessionPayload;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function deleteSession(sessionId: string): Promise<void> {
|
|
const redis = getRedis();
|
|
await redis.del(sessionKey(sessionId));
|
|
}
|
|
|