mirror of
https://github.com/lucasrcsantana/story-generator.git
synced 2025-12-17 05:47:52 +00:00
- Atualiza lógica de redirecionamento no LoginForm - Ajusta verificação de roles no useAuth hook - Melhora proteção de rotas no ProtectedRoute - Atualiza rotas para suportar diferentes perfis de usuário
36 lines
940 B
TypeScript
36 lines
940 B
TypeScript
import React from 'react';
|
|
import { Navigate } from 'react-router-dom';
|
|
import { useAuth } from '../../hooks/useAuth';
|
|
|
|
interface ProtectedRouteProps {
|
|
children: React.ReactNode;
|
|
allowedRoles?: string[];
|
|
}
|
|
|
|
export function ProtectedRoute({ children, allowedRoles = [] }: ProtectedRouteProps) {
|
|
const { user, loading, userRole } = useAuth();
|
|
|
|
if (loading) {
|
|
return <div>Carregando...</div>;
|
|
}
|
|
|
|
if (!user) {
|
|
return <Navigate to="/login/student" />;
|
|
}
|
|
|
|
if (allowedRoles.length > 0 && !allowedRoles.includes(userRole || '')) {
|
|
// Redirecionar para a página inicial apropriada baseado no papel do usuário
|
|
switch (userRole) {
|
|
case 'student':
|
|
return <Navigate to="/aluno" />;
|
|
case 'teacher':
|
|
return <Navigate to="/professor" />;
|
|
case 'school':
|
|
return <Navigate to="/dashboard" />;
|
|
default:
|
|
return <Navigate to="/" />;
|
|
}
|
|
}
|
|
|
|
return <>{children}</>;
|
|
}
|