react-garden
Version:
React + TypeScript + ThreeJS app using Material UI on NextJS, Apollo Client, GraphQL + WordPress REST APIs, for ThreeD web development.. a part of the threed.ai code family.
40 lines (34 loc) • 870 B
JavaScript
// ** React Imports
import { useEffect } from 'react'
// ** Next Imports
import { useRouter } from 'next/router'
// ** Hooks Import
import { useAuth } from '~/hooks/useAuth'
const AuthGuard = props => {
const { children, fallback } = props
const auth = useAuth()
const router = useRouter()
useEffect(
() => {
if (!router.isReady) {
return
}
if (auth.user === null && !window.localStorage.getItem('userData')) {
if (router.asPath !== '/') {
router.replace({
pathname: '/login',
query: { returnUrl: router.asPath }
})
} else {
router.replace('/login')
}
}
},
[router.route]
)
if (auth.loading || auth.user === null) {
return fallback
}
return <>{children}</>
}
export default AuthGuard