fix: improve auth guard and resolve blank page issue
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m22s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m22s
This commit is contained in:
@@ -30,8 +30,8 @@ export const Layout: React.FC<{ children: React.ReactNode }> = ({ children }) =>
|
|||||||
const fetchCurrentUser = async () => {
|
const fetchCurrentUser = async () => {
|
||||||
const storedUserId = localStorage.getItem('ctms_user_id');
|
const storedUserId = localStorage.getItem('ctms_user_id');
|
||||||
|
|
||||||
if (!storedUserId) {
|
if (!storedUserId || storedUserId === 'undefined' || storedUserId === 'null') {
|
||||||
navigate('/login');
|
navigate('/login', { replace: true });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,11 +42,13 @@ export const Layout: React.FC<{ children: React.ReactNode }> = ({ children }) =>
|
|||||||
} else {
|
} else {
|
||||||
// User ID exists in storage but not in DB (deleted user?)
|
// User ID exists in storage but not in DB (deleted user?)
|
||||||
localStorage.removeItem('ctms_user_id');
|
localStorage.removeItem('ctms_user_id');
|
||||||
navigate('/login');
|
localStorage.removeItem('ctms_tenant_id');
|
||||||
|
navigate('/login', { replace: true });
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Failed to fetch user:", err);
|
console.error("Failed to fetch user:", err);
|
||||||
// Fallback or error state
|
// If API fails completely, we might want to allow offline mode or retry
|
||||||
|
// For now, let's just log it.
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
fetchCurrentUser();
|
fetchCurrentUser();
|
||||||
@@ -55,13 +57,15 @@ export const Layout: React.FC<{ children: React.ReactNode }> = ({ children }) =>
|
|||||||
const handleLogout = () => {
|
const handleLogout = () => {
|
||||||
localStorage.removeItem('ctms_user_id');
|
localStorage.removeItem('ctms_user_id');
|
||||||
localStorage.removeItem('ctms_tenant_id');
|
localStorage.removeItem('ctms_tenant_id');
|
||||||
navigate('/login');
|
navigate('/login', { replace: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!currentUser) {
|
if (!currentUser) {
|
||||||
const storedUserId = localStorage.getItem('ctms_user_id');
|
const storedUserId = localStorage.getItem('ctms_user_id');
|
||||||
if (!storedUserId) return null; // Will redirect in useEffect
|
const isValid = storedUserId && storedUserId !== 'undefined' && storedUserId !== 'null';
|
||||||
return <div className="flex h-screen items-center justify-center bg-slate-50 text-slate-400">Autenticando...</div>;
|
|
||||||
|
if (!isValid) return null;
|
||||||
|
return <div className="flex h-screen items-center justify-center bg-slate-50 text-slate-400 font-medium">Iniciando sessão...</div>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isSuperAdmin = currentUser.role === 'super_admin';
|
const isSuperAdmin = currentUser.role === 'super_admin';
|
||||||
|
|||||||
Reference in New Issue
Block a user