fix: Correction des Protected Routes avec multi role

This commit is contained in:
Luc SORIGNET
2025-04-14 18:53:35 +02:00
parent 89b01b79db
commit dd0884bbce
3 changed files with 49 additions and 54 deletions

View File

@ -59,7 +59,6 @@ export default function Layout({
"settings": { "id": "settings", "name": t('settings'), "url": FE_ADMIN_SETTINGS_URL, "icon": Settings }
};
const [isLoading, setIsLoading] = useState(false);
const [isPopupVisible, setIsPopupVisible] = useState(false);
const pathname = usePathname();
@ -111,7 +110,7 @@ export default function Layout({
}, [pathname]);
return (
<ProtectedRoute requiredRight={RIGHTS.ADMIN}>
<ProtectedRoute requiredRight={[RIGHTS.ADMIN, RIGHTS.TEACHER]}>
<div className="flex min-h-screen bg-gray-50 relative">
{/* Retirer la condition !isLoading car on gère déjà le chargement au début */}
{/* Sidebar avec hauteur forcée */}

View File

@ -9,7 +9,6 @@ import StatCard from '@/components/StatCard';
import logger from '@/utils/logger';
import { fetchRegisterForms } from '@/app/actions/subscriptionAction';
import { fetchUpcomingEvents } from '@/app/actions/planningAction';
import { getSession } from 'next-auth/react';
import { useEstablishment } from '@/context/EstablishmentContext';
@ -41,65 +40,55 @@ export default function DashboardPage() {
const [classes, setClasses] = useState([]);
const [establishmentId, setEstablishmentId] = useState(null);
const { selectedEstablishmentId } = useEstablishment();
useEffect(() => {
getSession()
.then(session => {
setEstablishmentId(selectedEstablishmentId);
if (!selectedEstablishmentId) return;
setIsLoading(true); // Début du chargement
// Fetch des classes
fetchClasses(selectedEstablishmentId)
.then(data => {
setClasses(data);
logger.info('Classes fetched:', data);
const nbMaxStudents = data.reduce((acc, classe) => acc + classe.number_of_students, 0);
const nbStudents = data.reduce((acc, classe) => acc + classe.students.length, 0);
setStructureCapacity(nbMaxStudents);
setTotalStudents(nbStudents);
})
.catch(err => {
logger.error('Error fetching session:', err);
.catch(error => {
logger.error('Error fetching classes:', error);
});
}, []);
useEffect(() => {
if (establishmentId) {
// Fetch data for classes
fetchClasses(establishmentId).then(data => {
setClasses(data);
logger.info('Classes fetched:', data);
const nbMaxStudents = data.reduce((acc, classe) => acc + classe.number_of_students, 0);
const nbStudents = data.reduce((acc, classe) => acc + classe.students.length, 0);
setStructureCapacity(nbMaxStudents);
setTotalStudents(nbStudents);
})
.catch(error => {
logger.error('Error fetching classes:', error);
});
fetchRegisterForms().then(data => {
logger.info('Pending registrations fetched:', data);
setPendingRegistration(data.count);
})
.catch(error => {
logger.error('Error fetching pending registrations:', error);
});
fetchUpcomingEvents().then(data => {
setUpcomingEvents(data);
}).catch(error => {
logger.error('Error fetching upcoming events:', error);
});
// Simulation de chargement des données
setTimeout(() => {
setMonthlyStats({
inscriptions: [150, 180, 210, 245],
completionRate: 78
});
setIsLoading(false);
}, 1000);
}
}
, [establishmentId]);
// Fetch des formulaires d'inscription
fetchRegisterForms()
.then(data => {
logger.info('Pending registrations fetched:', data);
setPendingRegistration(data.count);
})
.catch(error => {
logger.error('Error fetching pending registrations:', error);
});
// Fetch des événements à venir
fetchUpcomingEvents()
.then(data => {
setUpcomingEvents(data);
})
.catch(error => {
logger.error('Error fetching upcoming events:', error);
})
.finally(() => {
setIsLoading(false); // Fin du chargement
});
}, [selectedEstablishmentId]);
if (isLoading) return <Loader />;
return (
<div className="p-6">
<div key={selectedEstablishmentId} className="p-6">
<h1 className="text-2xl font-bold mb-6">{t('dashboard')}</h1>
{/* Statistiques principales */}

View File

@ -1,13 +1,20 @@
import { useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useEstablishment } from '@/context/EstablishmentContext';
import { FE_USERS_LOGIN_URL,getRedirectUrlFromRole } from '@/utils/Url';
import { FE_USERS_LOGIN_URL, getRedirectUrlFromRole } from '@/utils/Url';
const ProtectedRoute = ({ children, requiredRight }) => {
const { user, profileRole } = useEstablishment();
const router = useRouter();
const hasRequiredRight = (profileRole === requiredRight);
let hasRequiredRight = false;
if(requiredRight && Array.isArray(requiredRight) ){
// Vérifier si l'utilisateur a le droit requis
hasRequiredRight = requiredRight.some((right) => profileRole === right);
}else{
hasRequiredRight = (profileRole === requiredRight);
}
// Vérifier si l'utilisateur a au moins un rôle correspondant au requiredRight
useEffect(() => {