Created
June 4, 2022 13:37
-
-
Save RomikMakavana/678a133523fac9d78eb1cd99aaeb2064 to your computer and use it in GitHub Desktop.
This file contains 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 { useNavigate } from 'react-router-dom'; | |
import AuthService from '../services/auth'; | |
const UnAuthGuard = ({ component }) => { | |
const [status, setStatus] = useState(false); | |
const navigate = useNavigate(); | |
useEffect(() => { | |
checkToken(); | |
}, [component]); | |
const checkToken = async () => { | |
try { | |
let user = await AuthService.getProfile(); | |
if (!user) { | |
localStorage.removeItem("token") | |
} else { | |
navigate(`/`); | |
} | |
setStatus(true); | |
} catch (error) { | |
navigate(`/`); | |
} | |
} | |
return status ? <React.Fragment>{component}</React.Fragment> : <React.Fragment></React.Fragment>; | |
} | |
export default UnAuthGuard; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment