feat: Enhance authentication middleware with reduced cache expiry, improved session validation, and global error handling for auth-related issues
This commit is contained in:
@@ -20,7 +20,7 @@ export default defineNuxtRouteMiddleware(async (to) => {
|
||||
// Use a cached auth state to avoid excessive API calls
|
||||
const nuxtApp = useNuxtApp();
|
||||
const cacheKey = 'auth:session:cache';
|
||||
const cacheExpiry = 15 * 60 * 1000; // 15 minutes cache (increased for better UX)
|
||||
const cacheExpiry = 2 * 60 * 1000; // 2 minutes cache - reduced to prevent stale auth state
|
||||
|
||||
// Check if we have a cached session
|
||||
const cachedSession = nuxtApp.payload.data?.[cacheKey];
|
||||
@@ -46,14 +46,22 @@ export default defineNuxtRouteMiddleware(async (to) => {
|
||||
try {
|
||||
// Check Keycloak authentication via session API with timeout and retries
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 10000); // 10 second timeout (increased from 5)
|
||||
const timeout = setTimeout(() => controller.abort(), 10000); // 10 second timeout
|
||||
|
||||
const sessionData = await $fetch('/api/auth/session', {
|
||||
signal: controller.signal,
|
||||
retry: 2, // Increased retry count
|
||||
retryDelay: 1000, // Increased retry delay
|
||||
retry: 2,
|
||||
retryDelay: 1000,
|
||||
onRetry: ({ retries }: { retries: number }) => {
|
||||
console.log(`[MIDDLEWARE] Retrying auth check (attempt ${retries + 1})`)
|
||||
},
|
||||
onResponseError({ response }) {
|
||||
// Clear cache on auth errors
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
console.log('[MIDDLEWARE] Auth error detected, clearing cache')
|
||||
delete nuxtApp.payload.data[cacheKey];
|
||||
delete nuxtApp.payload.data.authState;
|
||||
}
|
||||
}
|
||||
}) as any;
|
||||
|
||||
@@ -106,7 +114,7 @@ export default defineNuxtRouteMiddleware(async (to) => {
|
||||
if (error.name === 'AbortError' || error.code === 'ECONNREFUSED' || error.code === 'ETIMEDOUT') {
|
||||
console.log('[MIDDLEWARE] Network error, checking for recent cache');
|
||||
const recentCache = nuxtApp.payload.data?.[cacheKey];
|
||||
if (recentCache && recentCache.timestamp && (now - recentCache.timestamp) < 30 * 60 * 1000) { // 30 minutes grace period
|
||||
if (recentCache && recentCache.timestamp && (now - recentCache.timestamp) < 5 * 60 * 1000) { // 5 minutes grace period - reduced from 30
|
||||
console.log('[MIDDLEWARE] Using recent cache despite network error (age:', Math.round((now - recentCache.timestamp) / 1000), 'seconds)');
|
||||
if (recentCache.authenticated && recentCache.user) {
|
||||
// Store auth state for components
|
||||
@@ -119,8 +127,8 @@ export default defineNuxtRouteMiddleware(async (to) => {
|
||||
groups: recentCache.groups || []
|
||||
};
|
||||
|
||||
// Show a warning toast if cache is older than 10 minutes
|
||||
if ((now - recentCache.timestamp) > 10 * 60 * 1000) {
|
||||
// Show a warning toast if cache is older than 2 minutes
|
||||
if ((now - recentCache.timestamp) > 2 * 60 * 1000) {
|
||||
const toast = useToast();
|
||||
toast.warning('Network connectivity issue - using cached authentication');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user