23 lines
507 B
TypeScript
23 lines
507 B
TypeScript
import { randomUUID } from 'node:crypto';
|
|
|
|
interface Session {
|
|
username: string;
|
|
createdAt: number;
|
|
}
|
|
|
|
const sessions = new Map<string, Session>();
|
|
|
|
export function createSession(username: string): string {
|
|
const token = randomUUID();
|
|
sessions.set(token, { username, createdAt: Date.now() });
|
|
return token;
|
|
}
|
|
|
|
export function validateSession(token: string): Session | null {
|
|
return sessions.get(token) ?? null;
|
|
}
|
|
|
|
export function removeSession(token: string): void {
|
|
sessions.delete(token);
|
|
}
|