···2727 console.log("[logout] Session ID from cookie:", sessionId);
28282929 if (sessionId) {
3030+ // Get the DID before deleting
3131+ const userSession = await userSessions.get(sessionId);
3232+ const did = userSession?.did;
3333+3034 // Delete session from database
3135 await userSessions.del(sessionId);
3236 console.log("[logout] Deleted session from database");
+14-67
netlify/functions/session.ts
···1616 return key;
1717}
18181919-// ENHANCED: Two-tier cache system
2020-// Tier 1: In-memory cache for profile data (lives for function instance)
1919+// In-memory cache for profile
2120const profileCache = new Map<string, { data: any; timestamp: number }>();
2221const PROFILE_CACHE_TTL = 5 * 60 * 1000; // 5 minutes
2323-2424-// Tier 2: Session metadata cache (DID -> basic info, faster than full OAuth restore)
2525-const sessionMetadataCache = new Map<
2626- string,
2727- {
2828- did: string;
2929- lastSeen: number;
3030- profileFetchNeeded: boolean;
3131- }
3232->();
33223423export const handler: Handler = async (
3524 event: HandlerEvent,
···4938 };
5039 }
51405252- // OPTIMIZATION: Check session metadata cache first (avoids DB query)
5353- const cachedMetadata = sessionMetadataCache.get(sessionId);
5454- const now = Date.now();
4141+ // Check database for session
4242+ const userSession = await userSessions.get(sessionId);
55435656- let did: string;
4444+ if (!userSession) {
4545+ return {
4646+ statusCode: 401,
4747+ headers: { "Content-Type": "application/json" },
4848+ body: JSON.stringify({ error: "Invalid or expired session" }),
4949+ };
5050+ }
57515858- if (cachedMetadata && now - cachedMetadata.lastSeen < 60000) {
5959- // Session seen within last minute, trust the cache
6060- did = cachedMetadata.did;
6161- console.log("Session metadata from cache");
6262- } else {
6363- // Need to verify session from database
6464- const userSession = await userSessions.get(sessionId);
6565- if (!userSession) {
6666- // Clear stale cache entry
6767- sessionMetadataCache.delete(sessionId);
6868- return {
6969- statusCode: 401,
7070- headers: { "Content-Type": "application/json" },
7171- body: JSON.stringify({ error: "Invalid or expired session" }),
7272- };
7373- }
5252+ const did = userSession.did;
5353+ const now = Date.now();
74547575- did = userSession.did;
7676-7777- // Update session metadata cache
7878- sessionMetadataCache.set(sessionId, {
7979- did,
8080- lastSeen: now,
8181- profileFetchNeeded: true,
8282- });
8383-8484- // Cleanup: Remove old session metadata entries
8585- if (sessionMetadataCache.size > 200) {
8686- for (const [sid, meta] of sessionMetadataCache.entries()) {
8787- if (now - meta.lastSeen > 300000) {
8888- // 5 minutes
8989- sessionMetadataCache.delete(sid);
9090- }
9191- }
9292- }
9393- }
9494-9595- // Check profile cache (Tier 1)
5555+ // Check profile cache
9656 const cached = profileCache.get(did);
9757 if (cached && now - cached.timestamp < PROFILE_CACHE_TTL) {
9858 console.log("Returning cached profile for", did);
9999-100100- // Update session metadata last seen
101101- const meta = sessionMetadataCache.get(sessionId);
102102- if (meta) {
103103- meta.lastSeen = now;
104104- }
1055910660 return {
10761 statusCode: 200,
···181135 description: profile.data.description,
182136 };
183137184184- // Cache the profile data (Tier 1)
138138+ // Cache the profile data
185139 profileCache.set(did, {
186140 data: profileData,
187141 timestamp: now,
188142 });
189189-190190- // Update session metadata (Tier 2)
191191- const meta = sessionMetadataCache.get(sessionId);
192192- if (meta) {
193193- meta.lastSeen = now;
194194- meta.profileFetchNeeded = false;
195195- }
196143197144 // Clean up old profile cache entries
198145 if (profileCache.size > 100) {