File tree Expand file tree Collapse file tree 2 files changed +12
-8
lines changed Expand file tree Collapse file tree 2 files changed +12
-8
lines changed Original file line number Diff line number Diff line change 1- import { useState , useEffect } from 'react' ;
1+ import { useState , useEffect , useRef } from 'react' ;
22import { useRouter } from 'next/router' ;
33import { useAuth } from '@lib/context/auth-context' ;
44import { sleep } from '@lib/utils' ;
@@ -7,7 +7,6 @@ import type { LayoutProps } from './common-layout';
77
88export function AuthLayout ( { children } : LayoutProps ) : JSX . Element {
99 const [ pending , setPending ] = useState ( true ) ;
10-
1110 const { user, loading, error } = useAuth ( ) ;
1211 const { replace } = useRouter ( ) ;
1312
@@ -29,8 +28,13 @@ export function AuthLayout({ children }: LayoutProps): JSX.Element {
2928 } , [ user , loading ] ) ;
3029
3130 // If there's an auth error (user not found), redirect to login
31+ const hasRedirected = useRef ( false ) ;
3232 useEffect ( ( ) => {
33- if ( error ) void replace ( '/' ) ;
33+ if ( error && ! hasRedirected . current ) {
34+ hasRedirected . current = true ;
35+ alert ( error . message || 'An error occurred' ) ;
36+ void replace ( '/' ) ;
37+ }
3438 } , [ error , replace ] ) ;
3539
3640 if ( loading || pending ) return < Placeholder /> ;
Original file line number Diff line number Diff line change 1- import { useEffect } from 'react' ;
2- import { useRouter } from 'next/router' ;
3- import { useAuth } from '@lib/context/auth-context' ;
4- import type { User } from '@lib/types/user' ;
1+ import { useAuth } from "@lib/context/auth-context" ;
2+ import type { User } from "@lib/types/user" ;
3+ import { useRouter } from "next/router" ;
4+ import { useEffect } from "react" ;
55
66export function useRequireAuth ( redirectUrl ?: string ) : User | null {
77 const { user, loading } = useAuth ( ) ;
88 const { replace } = useRouter ( ) ;
99
1010 useEffect ( ( ) => {
11- if ( ! loading && ! user ) void replace ( redirectUrl ?? '/' ) ;
11+ if ( ! loading && ! user ) void replace ( redirectUrl ?? "/" ) ;
1212 // eslint-disable-next-line react-hooks/exhaustive-deps
1313 } , [ user , loading ] ) ;
1414
You can’t perform that action at this time.
0 commit comments