Untitled
unknown
plain_text
2 years ago
1.8 kB
4
Indexable
import TOASTMSG from '@/constants/global/toastMessages'; import toast from '@/functions/global/toast'; import useLocalStorage from '@/hooks/global/localStorage'; import { UserAuthType } from '@/types/global/userAuth.type'; import { useRouter } from 'next/router'; import { FC, ReactNode, useEffect, useRef } from 'react'; interface IProps { children: ReactNode; } const RouteGuard: FC<IProps> = ({ ...props }) => { // for check is logginedUser const [localStorageUserAuthData, setLocalStorageUserAuthData] = useLocalStorage<undefined | null | UserAuthType>('userAuthdata', null); const router = useRouter(); const { pathname } = router; const isMountedSigninRouteRef = useRef(false); useEffect((): (() => void) | void => { // guard for logginedUser if ( localStorageUserAuthData !== null && localStorageUserAuthData !== undefined ) { // current route is ['/auth/forget-password', '/auth/sign-up'] and loggined user ===> re-direct to profile for logout //! '/auth/sign-in' guard is in 'src\pages\auth\sign-in\index.tsx line 22' if ( pathname === '/auth/forget-password' || pathname === '/auth/sign-up' ) { toast(TOASTMSG.routes.auth.global.pleaseLogout); router.push(`/profile/?navigate=${pathname}`); } if (pathname === '/auth/sign-in') { if (!isMountedSigninRouteRef.current) { isMountedSigninRouteRef.current = true; console.log('hi'); if (localStorageUserAuthData) { toast(TOASTMSG.routes.auth.global.pleaseLogout); router.push(`/profile/?navigate=${pathname}`); } } } } }, [pathname, localStorageUserAuthData]); return <>{props.children}</>; }; export default RouteGuard;
Editor is loading...