Spaces:
Sleeping
Sleeping
Auto-validate and refresh stale sessions on page load
Browse files- 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 |
-
//
|
| 207 |
-
|
| 208 |
-
setSessionKey(savedSessionKey)
|
| 209 |
-
setSessionInitialized(true)
|
| 210 |
-
console.log('✅ Loaded existing session:', savedSessionId)
|
| 211 |
-
} else {
|
| 212 |
-
// Create new session automatically
|
| 213 |
try {
|
| 214 |
-
const
|
| 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 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
setSessionKey(data.session.key)
|
| 230 |
setSessionInitialized(true)
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
console.log('✅ Auto-created new session:', data.session.id)
|
| 237 |
}
|
| 238 |
} catch (error) {
|
| 239 |
-
console.error('Failed to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|