mirror of
https://github.com/lucasrcsantana/story-generator.git
synced 2025-12-17 05:47:52 +00:00
fix: ajusta verificação de roles no ProtectedRoute
- Adiciona logs detalhados para debug do fluxo de autenticação - Pega role diretamente dos metadados do usuário - Simplifica lógica de verificação de roles com switch case - Melhora mensagens de debug para identificar problemas de acesso
This commit is contained in:
parent
441b55535e
commit
c8420421eb
@ -11,6 +11,10 @@ export function ProtectedRoute({ children, allowedRoles = [] }: ProtectedRoutePr
|
|||||||
const { user, loading, userRole } = useAuth();
|
const { user, loading, userRole } = useAuth();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|
||||||
|
console.log('ProtectedRoute - User:', user?.user_metadata);
|
||||||
|
console.log('ProtectedRoute - UserRole do contexto:', userRole);
|
||||||
|
console.log('ProtectedRoute - Roles permitidas:', allowedRoles);
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return <div>Carregando...</div>;
|
return <div>Carregando...</div>;
|
||||||
}
|
}
|
||||||
@ -20,27 +24,30 @@ export function ProtectedRoute({ children, allowedRoles = [] }: ProtectedRoutePr
|
|||||||
return <Navigate to="/login/school" state={{ from: location }} replace />;
|
return <Navigate to="/login/school" state={{ from: location }} replace />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pegar o role diretamente dos metadados do usuário
|
||||||
|
const currentRole = user.user_metadata?.role;
|
||||||
|
console.log('ProtectedRoute - Role dos metadados:', currentRole);
|
||||||
|
|
||||||
// Se não houver roles requeridas, permite acesso
|
// Se não houver roles requeridas, permite acesso
|
||||||
if (allowedRoles.length === 0) {
|
if (allowedRoles.length === 0) {
|
||||||
return <>{children}</>;
|
return <>{children}</>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Se o usuário não tiver o role necessário
|
// Se o usuário não tiver o role necessário
|
||||||
if (!allowedRoles.includes(userRole || '')) {
|
if (!allowedRoles.includes(currentRole)) {
|
||||||
console.log('Role atual:', userRole);
|
console.log('ProtectedRoute - Acesso negado');
|
||||||
console.log('Roles permitidas:', allowedRoles);
|
|
||||||
|
|
||||||
// Redireciona para a página apropriada
|
// Redireciona para a página apropriada
|
||||||
if (userRole === 'school') {
|
switch (currentRole) {
|
||||||
return <Navigate to="/dashboard" replace />;
|
case 'school':
|
||||||
} else if (userRole === 'teacher') {
|
return <Navigate to="/dashboard" replace />;
|
||||||
return <Navigate to="/professor" replace />;
|
case 'teacher':
|
||||||
} else if (userRole === 'student') {
|
return <Navigate to="/professor" replace />;
|
||||||
return <Navigate to="/aluno" replace />;
|
case 'student':
|
||||||
|
return <Navigate to="/aluno" replace />;
|
||||||
|
default:
|
||||||
|
return <Navigate to="/" replace />;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Se não tiver role definido, volta para home
|
|
||||||
return <Navigate to="/" replace />;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return <>{children}</>;
|
return <>{children}</>;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user