Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 78x 142x 425x 425x 191x 70x 121x 8x 113x 4x 109x | 'use client' import { RedirectType } from 'next/dist/client/components/redirect' import { redirect } from 'next/navigation' import { routes } from '../../services/routes/routes' import { useAppSelector } from '../../store/hooks' import { Loading } from '../Loading/Loading' /** * Use this HOC to protect routes from unauthorized access. * Protected routes are only accessible when the user is authenticated. * Guest routes are only accessible when the user is not authenticated. * @param Component The component to protect * @param routeType The type of route protected or guest. * @returns The protected component or redirects. */ const isAuth = <P,>(Component: React.ComponentType<P>, routeType: 'protected' | 'guest') => { // eslint-disable-next-line return function NewComponent(props: any) { const isInitialized = useAppSelector((state) => state.auth.isInitialized) const isAuthenticated = useAppSelector((state) => state.auth.isAuthenticated) if (!isInitialized) return ( <div className="container mt-5"> <Loading /> </div> ) if (routeType === 'protected' && !isAuthenticated && isInitialized) { redirect(routes.account.login(), RedirectType.replace) } if (routeType === 'guest' && isAuthenticated && isInitialized) { redirect(routes.profile.myLastWills, RedirectType.replace) } return <Component {...props} /> } } export default isAuth |