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
This commit is contained in:
michaelschal committed 2025-08-13 21:28:29 +00:00
1 parent 0d903315e8
commit f42379b330
2 files changed
+24 -1

No files matched your search

@@ -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 (
<div className="min-h-screen w-full flex items-center justify-center bg-surface">
<div className="text-center">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-primary mx-auto mb-4"></div>
<p className="text-gray-600">Redirection...</p>
</div>
</div>
);
}
return (
<Switch>
<Route path="/auth" component={AuthPage} />
+10 -1
View File
@@ -54,6 +54,11 @@ export const getQueryFn: <T>(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;