File size: 569 Bytes
57da3ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { Navigate } from 'react-router-dom';
import { useStore } from '../store';

export function ProtectedRoute({ children, requiredRole }) {
  const { currentUser } = useStore();

  if (!currentUser) {
    // Redirect to the registration/auth page if not logged in
    return <Navigate to="/" replace />;
  }

  if (requiredRole && currentUser.role !== requiredRole) {
    // If they have the wrong role, redirect back to their appropriate dashboard
    return <Navigate to={currentUser.role === 'buyer' ? '/buyer' : '/seller'} replace />;
  }

  return children;
}