forked from CSI-KJSCE/appointment_to_examiner
protected route
This commit is contained in:
12
client/src/components/PrivateRoute.js
Normal file
12
client/src/components/PrivateRoute.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import React from 'react';
|
||||
import { Navigate } from 'react-router-dom'; // Use Navigate for redirect
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
const PrivateRoute = ({ element: Element, ...rest }) => {
|
||||
const token = Cookies.get("token");
|
||||
|
||||
// If token exists, render the element. Otherwise, redirect to the login page
|
||||
return token ? Element : <Navigate to="/" />;
|
||||
};
|
||||
|
||||
export default PrivateRoute;
|
||||
Reference in New Issue
Block a user