From f42379b330aeb4dcd566ad627d09b9c7b3159eeb Mon Sep 17 00:00:00 2001 From: michaelschal <35957947-michaelschal@users.noreply.replit.com> Date: Wed, 13 Aug 2025 21:28:29 +0000 Subject: [PATCH] Automatically redirect unauthenticated users to the login page Implement automatic redirection to the authentication page for unauthenticated users, both client-side and for API 401 responses, to prevent access to protected routes or data. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 7c468936-2a88-4686-ac0a-84921a45222d Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/7c468936-2a88-4686-ac0a-84921a45222d/jxMXcbq --- client/src/components/RouterProduction.tsx | 14 ++++++++++++++ client/src/lib/queryClient.ts | 11 ++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/client/src/components/RouterProduction.tsx b/client/src/components/RouterProduction.tsx index 53f7a54..6a7506f 100644 --- a/client/src/components/RouterProduction.tsx +++ b/client/src/components/RouterProduction.tsx @@ -54,6 +54,20 @@ function RouterProduction() { } if (!isAuthenticated) { + // Redirection automatique vers la page d'authentification + if (typeof window !== 'undefined' && window.location.pathname !== '/auth') { + console.log('🔄 User not authenticated, redirecting to auth page'); + window.location.href = '/auth'; + return ( +
+
+
+

Redirection...

+
+
+ ); + } + return ( diff --git a/client/src/lib/queryClient.ts b/client/src/lib/queryClient.ts index fa5743f..8befad5 100644 --- a/client/src/lib/queryClient.ts +++ b/client/src/lib/queryClient.ts @@ -54,6 +54,11 @@ export const getQueryFn: (options: { }); if (unauthorizedBehavior === "returnNull" && res.status === 401) { + // Redirection automatique vers la page d'authentification si 401 + if (typeof window !== 'undefined' && window.location.pathname !== '/auth') { + console.log('🔄 API 401 - Redirecting to auth page'); + window.location.href = '/auth'; + } return null; } @@ -72,8 +77,12 @@ export const queryClient = new QueryClient({ staleTime: 30 * 1000, // 30 secondes de cache pour de meilleures performances gcTime: 10 * 60 * 1000, // 10 minutes retry: (failureCount, error: any) => { - // Ne pas retry les erreurs d'authentification + // Ne pas retry les erreurs d'authentification et rediriger if (error?.message?.includes('401') || error?.message?.includes('Unauthorized')) { + if (typeof window !== 'undefined' && window.location.pathname !== '/auth') { + console.log('🔄 Query 401 - Redirecting to auth page'); + window.location.href = '/auth'; + } return false; } return failureCount < 2;