feat: Address 404 errors and session management issues, improve authorization middleware to use cached auth state, and adjust auth refresh plugin for better session validation
This commit is contained in:
@@ -209,13 +209,14 @@ export default defineNuxtPlugin(() => {
|
||||
})
|
||||
}
|
||||
|
||||
// Add periodic session validation (every 2 minutes with offset)
|
||||
// Add periodic session validation (every 5 minutes instead of 2)
|
||||
let validationInterval: NodeJS.Timeout | null = null
|
||||
let isValidating = false // Prevent concurrent validations
|
||||
let failureCount = 0 // Track consecutive failures
|
||||
|
||||
onMounted(() => {
|
||||
// Add random offset to prevent all clients checking at once
|
||||
const randomOffset = Math.floor(Math.random() * 5000) // 0-5 seconds
|
||||
const randomOffset = Math.floor(Math.random() * 10000) // 0-10 seconds
|
||||
|
||||
setTimeout(() => {
|
||||
validationInterval = setInterval(async () => {
|
||||
@@ -233,17 +234,34 @@ export default defineNuxtPlugin(() => {
|
||||
})
|
||||
|
||||
if (!response.ok || response.status === 401) {
|
||||
console.log('[AUTH_REFRESH] Session invalid during periodic check')
|
||||
clearInterval(validationInterval!)
|
||||
await navigateTo('/login')
|
||||
failureCount++
|
||||
console.log(`[AUTH_REFRESH] Session check failed (attempt ${failureCount}/3)`)
|
||||
|
||||
// Only logout after 3 consecutive failures
|
||||
if (failureCount >= 3) {
|
||||
console.log('[AUTH_REFRESH] Session invalid after 3 attempts, redirecting to login')
|
||||
clearInterval(validationInterval!)
|
||||
await navigateTo('/login')
|
||||
}
|
||||
} else {
|
||||
// Reset failure count on success
|
||||
failureCount = 0
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[AUTH_REFRESH] Periodic validation error:', error)
|
||||
// Don't logout on network errors - let middleware handle it
|
||||
// But count it as a failure for resilience
|
||||
failureCount++
|
||||
|
||||
if (failureCount >= 3) {
|
||||
console.log('[AUTH_REFRESH] Too many validation errors, redirecting to login')
|
||||
clearInterval(validationInterval!)
|
||||
await navigateTo('/login')
|
||||
}
|
||||
} finally {
|
||||
isValidating = false
|
||||
}
|
||||
}, 2 * 60 * 1000) // Keep at 2 minutes
|
||||
}, 5 * 60 * 1000) // Changed to 5 minutes to avoid conflicts with 3-minute cache
|
||||
}, randomOffset)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user