Browse Source

Improve auth error handling to prevent redirect loops

drawing-pad
Stephanie Gredell 1 month ago
parent
commit
66bda7554a
  1. 34
      frontend/src/hooks/useAuth.tsx

34
frontend/src/hooks/useAuth.tsx

@ -18,18 +18,40 @@ export function AuthProvider({ children }: { children: ReactNode }) {
useEffect(() => { useEffect(() => {
// Check if user is logged in on mount // Check if user is logged in on mount
const checkAuth = async () => {
const token = localStorage.getItem('access_token'); const token = localStorage.getItem('access_token');
if (token) { if (!token) {
authApi.getCurrentUser() setLoading(false);
.then((response: any) => setUser(response.data)) return;
.catch(() => { }
try {
// getCurrentUser will trigger token refresh via interceptor if needed
const response: any = await authApi.getCurrentUser();
setUser(response.data);
} catch (error: any) {
// If we get here, either:
// 1. Token refresh failed (refresh token expired/invalid)
// 2. Network error
// 3. User doesn't exist (unlikely)
// Only clear auth if it's an auth-related error
// Don't clear on network errors - user might still be authenticated
const errorCode = error?.error?.code || error?.response?.data?.error?.code;
if (errorCode === 'UNAUTHORIZED' || errorCode === 'REFRESH_ERROR' || errorCode === 'INVALID_TOKEN') {
setUser(null); setUser(null);
localStorage.removeItem('access_token'); localStorage.removeItem('access_token');
})
.finally(() => setLoading(false));
} else { } else {
// For other errors (network, etc.), keep the token but don't set user
// This prevents redirect loops while allowing retry
console.warn('Failed to get current user (non-auth error):', error);
}
} finally {
setLoading(false); setLoading(false);
} }
};
checkAuth();
}, []); }, []);
const login = async (username: string, password: string) => { const login = async (username: string, password: string) => {

Loading…
Cancel
Save