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' ;
2
2
import { useRouter } from 'next/router' ;
3
3
import { useAuth } from '@lib/context/auth-context' ;
4
4
import { sleep } from '@lib/utils' ;
@@ -7,7 +7,6 @@ import type { LayoutProps } from './common-layout';
7
7
8
8
export function AuthLayout ( { children } : LayoutProps ) : JSX . Element {
9
9
const [ pending , setPending ] = useState ( true ) ;
10
-
11
10
const { user, loading, error } = useAuth ( ) ;
12
11
const { replace } = useRouter ( ) ;
13
12
@@ -29,8 +28,13 @@ export function AuthLayout({ children }: LayoutProps): JSX.Element {
29
28
} , [ user , loading ] ) ;
30
29
31
30
// If there's an auth error (user not found), redirect to login
31
+ const hasRedirected = useRef ( false ) ;
32
32
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
+ }
34
38
} , [ error , replace ] ) ;
35
39
36
40
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" ;
5
5
6
6
export function useRequireAuth ( redirectUrl ?: string ) : User | null {
7
7
const { user, loading } = useAuth ( ) ;
8
8
const { replace } = useRouter ( ) ;
9
9
10
10
useEffect ( ( ) => {
11
- if ( ! loading && ! user ) void replace ( redirectUrl ?? '/' ) ;
11
+ if ( ! loading && ! user ) void replace ( redirectUrl ?? "/" ) ;
12
12
// eslint-disable-next-line react-hooks/exhaustive-deps
13
13
} , [ user , loading ] ) ;
14
14
You can’t perform that action at this time.
0 commit comments