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 { useRouter } from 'next/router'; | |
import { useContext, useEffect } from 'react' | |
import AuthContext from '../store/auth.context' | |
import Loading from './Loading'; | |
import Login from './Login'; | |
function AuthCheck({children}: any) { | |
const {user, loading} = useContext(AuthContext); | |
const router = useRouter(); | |
useEffect(() => { | |
if(user && !loading && router.pathname === '/') { | |
router.replace('/todo'); | |
} | |
}, [loading]); | |
if(user && !loading && router.pathname !== '/') { | |
return children; | |
} | |
else if (!user && !loading) { | |
return <Login/>; | |
}else { | |
return <Loading/>; | |
} | |
} | |
export default AuthCheck |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment