Skip to content

Instantly share code, notes, and snippets.

@ShekMak
Created February 20, 2022 17:56
Embed
What would you like to do?
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