24 lines
684 B
JavaScript
24 lines
684 B
JavaScript
import { Navigate } from 'react-router-dom'
|
|
import { useAuth } from '../context/AuthContext'
|
|
import { homeFor, INITIAL_PASSWORD_PATH } from '../config/roles'
|
|
|
|
export default function ProtectedRoute({ children, roles }) {
|
|
const { user } = useAuth()
|
|
|
|
if (!user) {
|
|
return <Navigate to="/connexion" replace />
|
|
}
|
|
|
|
// Mot de passe provisoire : aucune page protégée tant qu'il n'est pas
|
|
// remplacé (le backend refuse de toute façon les appels à l'API).
|
|
if (user.mustChangePassword) {
|
|
return <Navigate to={INITIAL_PASSWORD_PATH} replace />
|
|
}
|
|
|
|
if (roles && !roles.includes(user.role)) {
|
|
return <Navigate to={homeFor(user)} replace />
|
|
}
|
|
|
|
return children
|
|
}
|