Created
October 18, 2022 02:35
-
-
Save tararoutray/9220b4f06e2f588c1d14a700d19d4a8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useState } from "react"; | |
import { Route, useNavigate } from "react-router-dom"; | |
const ProtectedRoute = (props) => { | |
const navigate = useNavigate(); | |
const [isLoggedIn, setIsLoggedIn] = useState(false); | |
const checkUserToken = () => { | |
const userToken = localStorage.getItem('user-token'); | |
if (!userToken || userToken === 'undefined') { | |
setIsLoggedIn(false); | |
return navigate('/auth/login'); | |
} | |
setIsLoggedIn(true); | |
} | |
useEffect(() => { | |
checkUserToken(); | |
}, [isLoggedIn]); | |
return ( | |
<React.Fragment> | |
{ | |
isLoggedIn ? props.children : null | |
} | |
</React.Fragment> | |
); | |
} | |
export default ProtectedRoute; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment