diff --git a/server.js b/server.js index a40ddfa..e5c8efb 100644 --- a/server.js +++ b/server.js @@ -17,6 +17,7 @@ const SIXTY_DAYS_MS = 60 * 24 * 60 * 60 * 1000; const app = express(); const port = process.env.PORT || 3000; const storeRefreshJobs = new Map(); +const cachedStoreSnapshots = new Map(); app.use(cors()); app.use(express.json({ limit: '1mb' })); @@ -302,7 +303,7 @@ app.post('/api/auth/login', async (req, res) => { const existingCredentials = credentialStore.get(profile.id); const existingToken = existingCredentials?.token; - const previousSession = existingToken ? sessionStore.get(existingToken) : null; + const previousSession = existingToken ? sessionStore.get(existingToken) : null; if (existingToken) { sessionStore.delete(existingToken); } @@ -319,6 +320,9 @@ app.post('/api/auth/login', async (req, res) => { scheduleConfig(session.id, config, settings); if (previousSession?.storesCache) { sessionStore.update(session.id, { storesCache: previousSession.storesCache }); + } else if (cachedStoreSnapshots.has(profile.id)) { + sessionStore.update(session.id, { storesCache: cachedStoreSnapshots.get(profile.id) }); + cachedStoreSnapshots.delete(profile.id); } const currentSession = sessionStore.get(session.id); const needsRefresh = !isStoreCacheFresh(currentSession); @@ -341,6 +345,9 @@ app.post('/api/auth/login', async (req, res) => { }); app.post('/api/auth/logout', requireAuth, (req, res) => { + if (req.session?.profile?.id && req.session?.storesCache) { + cachedStoreSnapshots.set(req.session.profile.id, req.session.storesCache); + } sessionStore.delete(req.session.id); credentialStore.remove(req.session.profile.id); storeRefreshJobs.delete(req.session.id);