File size: 897 Bytes
29bcc71
 
 
 
 
 
8c56188
29bcc71
 
0826f71
29bcc71
 
 
 
8356c2e
8c56188
29bcc71
 
0826f71
29bcc71
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { Navigate } from 'react-router-dom'
import { useAuth } from '../../context/AuthContext'
import { PageLoader } from './Spinner'

export function ProtectedRoute({ children, role }) {
  const { user, loading } = useAuth()
  if (loading) return <PageLoader />
  if (!user) return <Navigate to="/login" replace />
  if (role === 'admin' && user.role !== 'admin') return <Navigate to="/dashboard" replace />
  if (role === 'aspirant' && !['admin', 'aspirant'].includes(user.role)) return <Navigate to="/pending" replace />
  return children
}

export function GuestRoute({ children }) {
  const { user, loading } = useAuth()
  if (loading) return <PageLoader />
  if (user) {
    if (user.role === 'admin') return <Navigate to="/admin" replace />
    if (user.role === 'aspirant') return <Navigate to="/dashboard" replace />
    return <Navigate to="/pending" replace />
  }
  return children
}