Reubencf commited on
Commit
86fdeeb
·
1 Parent(s): 18fa9bc

Auto-validate and refresh stale sessions on page load

Browse files
Files changed (1) hide show
  1. app/components/Desktop.tsx +44 -26
app/components/Desktop.tsx CHANGED
@@ -203,41 +203,59 @@ export function Desktop() {
203
  const savedSessionKey = localStorage.getItem('reubenOS_sessionKey')
204
 
205
  if (savedSessionId && savedSessionKey) {
206
- // Use existing session
207
- setUserSession(savedSessionId)
208
- setSessionKey(savedSessionKey)
209
- setSessionInitialized(true)
210
- console.log('✅ Loaded existing session:', savedSessionId)
211
- } else {
212
- // Create new session automatically
213
  try {
214
- const response = await fetch('/api/sessions/create', {
215
  method: 'POST',
216
  headers: { 'Content-Type': 'application/json' },
217
- body: JSON.stringify({
218
- metadata: {
219
- createdAt: new Date().toISOString(),
220
- autoCreated: true
221
- }
222
- })
223
  })
 
224
 
225
- const data = await response.json()
226
-
227
- if (data.success) {
228
- setUserSession(data.session.id)
229
- setSessionKey(data.session.key)
230
  setSessionInitialized(true)
231
-
232
- // Save to localStorage
233
- localStorage.setItem('reubenOS_sessionId', data.session.id)
234
- localStorage.setItem('reubenOS_sessionKey', data.session.key)
235
-
236
- console.log('✅ Auto-created new session:', data.session.id)
237
  }
238
  } catch (error) {
239
- console.error('Failed to create session:', error)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  }
 
 
241
  }
242
  }
243
 
 
203
  const savedSessionKey = localStorage.getItem('reubenOS_sessionKey')
204
 
205
  if (savedSessionId && savedSessionKey) {
206
+ // Validate the saved session first
207
+ console.log('🔍 Validating existing session:', savedSessionId)
 
 
 
 
 
208
  try {
209
+ const validateResponse = await fetch('/api/sessions/verify', {
210
  method: 'POST',
211
  headers: { 'Content-Type': 'application/json' },
212
+ body: JSON.stringify({ sessionKey: savedSessionKey })
 
 
 
 
 
213
  })
214
+ const validateData = await validateResponse.json()
215
 
216
+ if (validateData.success && validateData.valid) {
217
+ // Session is still valid - use it
218
+ setUserSession(savedSessionId)
219
+ setSessionKey(savedSessionKey)
 
220
  setSessionInitialized(true)
221
+ console.log('✅ Existing session is valid:', savedSessionId)
222
+ return
223
+ } else {
224
+ console.log('⚠️ Existing session is invalid, creating new one...')
 
 
225
  }
226
  } catch (error) {
227
+ console.error('Failed to validate session, creating new one:', error)
228
+ }
229
+ }
230
+
231
+ // Create new session (either no saved session or validation failed)
232
+ try {
233
+ const response = await fetch('/api/sessions/create', {
234
+ method: 'POST',
235
+ headers: { 'Content-Type': 'application/json' },
236
+ body: JSON.stringify({
237
+ metadata: {
238
+ createdAt: new Date().toISOString(),
239
+ autoCreated: true
240
+ }
241
+ })
242
+ })
243
+
244
+ const data = await response.json()
245
+
246
+ if (data.success) {
247
+ setUserSession(data.session.id)
248
+ setSessionKey(data.session.key)
249
+ setSessionInitialized(true)
250
+
251
+ // Save to localStorage
252
+ localStorage.setItem('reubenOS_sessionId', data.session.id)
253
+ localStorage.setItem('reubenOS_sessionKey', data.session.key)
254
+
255
+ console.log('✅ Created fresh session:', data.session.id)
256
  }
257
+ } catch (error) {
258
+ console.error('Failed to create session:', error)
259
  }
260
  }
261