MCPcopy Create free account
hub / github.com/BingyanStudio/github-analyzer / SessionStore

Class SessionStore

api/src/services/sessionStore.ts:9–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7const SESSION_TTL = parseInt(process.env.SESSION_TTL || "604800");
8
9class SessionStore {
10 /**
11 * Save session data to Redis
12 * @param sessionId The session ID
13 * @param session Session data
14 * @returns Promise that resolves when the session is saved
15 */
16 async saveSession(sessionId: string, session: UserSession): Promise<void> {
17 await redis.set(`session:${sessionId}`, JSON.stringify(session));
18 await redis.expire(`session:${sessionId}`, SESSION_TTL);
19 }
20
21 /**
22 * Get session data from Redis
23 * @param sessionId The session ID
24 * @returns Session data or null if not found
25 */
26 async getSession(sessionId: string): Promise<UserSession | null> {
27 const data = await redis.get(`session:${sessionId}`);
28 if (!data) return null;
29
30 try {
31 const session = JSON.parse(data) as UserSession;
32
33 // Convert date strings back to Date objects
34 if (session.expiresAt) {
35 session.expiresAt = new Date(session.expiresAt);
36 }
37 if (session.createdAt) {
38 session.createdAt = new Date(session.createdAt);
39 }
40
41 return session;
42 } catch (error) {
43 console.error("Error parsing session data:", error);
44 return null;
45 }
46 }
47
48 /**
49 * Remove a session from Redis
50 * @param sessionId The session ID
51 */
52 async removeSession(sessionId: string): Promise<void> {
53 await redis.del(`session:${sessionId}`);
54 }
55
56 /**
57 * Update an existing session with new data
58 * @param sessionId The session ID
59 * @param updatedData The updated session data
60 */
61 async updateSession(
62 sessionId: string,
63 updatedData: Partial<UserSession>
64 ): Promise<void> {
65 const session = await this.getSession(sessionId);
66 if (!session) return;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected