All files / src/utils session-manager.ts

0% Statements 0/20
0% Branches 0/7
0% Functions 0/5
0% Lines 0/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51                                                                                                     
import { v4 as uuidv4 } from 'uuid';
import { Session } from '../types';
 
// Session storage for verified users
export const verifiedSessions = new Map<string, Session>();
 
export class SessionManager {
  static generateSessionId(): string {
    return uuidv4();
  }
 
  static createSession(phoneNumber: string, userData: any = {}): string {
    const sessionId = this.generateSessionId();
    const session: Session = {
      id: sessionId,
      phoneNumber,
      userData,
      createdAt: Date.now(),
      expiresAt: Date.now() + (1 * 60 * 1000) // 1 minute
    };
    
    verifiedSessions.set(sessionId, session);
    return sessionId;
  }
 
  static getSession(sessionId: string): Session | null {
    const session = verifiedSessions.get(sessionId);
    if (!session) return null;
    
    if (Date.now() > session.expiresAt) {
      verifiedSessions.delete(sessionId);
      return null;
    }
    
    return session;
  }
 
  static deleteSession(sessionId: string): void {
    verifiedSessions.delete(sessionId);
  }
 
  static cleanupExpiredSessions(): void {
    const now = Date.now();
    for (const [sessionId, session] of verifiedSessions) {
      if (now > session.expiresAt) {
        verifiedSessions.delete(sessionId);
      }
    }
  }
}