18 lines
484 B
JavaScript
18 lines
484 B
JavaScript
import { Navigate } from 'react-router-dom'
|
|
import { useAuth } from '../context/AuthContext'
|
|
|
|
export default function ProtectedRoute({ children, roles }) {
|
|
const { user } = useAuth()
|
|
|
|
if (!user) {
|
|
return <Navigate to="/connexion" replace />
|
|
}
|
|
|
|
if (roles && !roles.includes(user.role)) {
|
|
const redirects = { ADMIN: '/admin', EMPLOYEE: '/employe', CLIENT: '/mes-gains' }
|
|
return <Navigate to={redirects[user.role] || '/connexion'} replace />
|
|
}
|
|
|
|
return children
|
|
}
|