Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ** React Imports
- import { ReactNode, ReactElement, useEffect } from 'react'
- // ** Next Imports
- import { useRouter } from 'next/router'
- // ** Hooks Import
- import { useAuth } from 'src/hooks/useAuth'
- interface AuthGuardProps {
- children: ReactNode
- fallback: ReactElement | null
- }
- const AuthGuard = (props: AuthGuardProps) => {
- const { children, fallback } = props
- const auth = useAuth()
- const router = useRouter()
- useEffect(
- () => {
- if (!router.isReady) {
- return
- }
- if (auth.user === null) {
- if (router.asPath !== '/') {
- router.replace({
- pathname: '/login',
- query: { returnUrl: router.asPath }
- })
- } else {
- router.replace('/login')
- }
- }
- },
- // eslint-disable-next-line react-hooks/exhaustive-deps
- [router.route]
- )
- if (auth.loading || auth.user === null) {
- return fallback
- }
- return <>{children}</>
- }
- export default AuthGuard
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement