protected route

This commit is contained in:
amNobodyyy
2025-01-28 12:03:22 +05:30
parent b2f44b66a3
commit ef4355dfb8
5 changed files with 41 additions and 18 deletions

View 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;