File tree 6 files changed +23
-31
lines changed
6 files changed +23
-31
lines changed Original file line number Diff line number Diff line change 1
- import { anonymousRegex } from '@/lib/constants' ;
2
1
import type { NextAuthConfig } from 'next-auth' ;
3
2
4
3
export const authConfig = {
@@ -10,29 +9,5 @@ export const authConfig = {
10
9
// added later in auth.ts since it requires bcrypt which is only compatible with Node.js
11
10
// while this file is also used in non-Node.js environments
12
11
] ,
13
- callbacks : {
14
- authorized ( { auth, request : { nextUrl } } ) {
15
- const isLoggedIn = ! ! auth ?. user ;
16
- const isAnonymousUser = anonymousRegex . test ( auth ?. user ?. email ?? '' ) ;
17
-
18
- const isOnLoginPage = nextUrl . pathname . startsWith ( '/login' ) ;
19
- const isOnRegisterPage = nextUrl . pathname . startsWith ( '/register' ) ;
20
-
21
- // If logged in, redirect to home page
22
- if (
23
- isLoggedIn &&
24
- ! isAnonymousUser &&
25
- ( isOnLoginPage || isOnRegisterPage )
26
- ) {
27
- return Response . redirect ( new URL ( '/' , nextUrl as unknown as URL ) ) ;
28
- }
29
-
30
- // Always allow access to register and login pages
31
- if ( isOnRegisterPage || isOnLoginPage ) {
32
- return true ;
33
- }
34
-
35
- return true ;
36
- } ,
37
- } ,
12
+ callbacks : { } ,
38
13
} satisfies NextAuthConfig ;
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ export const {
29
29
const passwordsMatch = await compare ( password , user . password ) ;
30
30
31
31
if ( ! passwordsMatch ) return null ;
32
+
32
33
return user ;
33
34
} ,
34
35
} ) ,
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import { AuthForm } from '@/components/auth-form';
9
9
import { SubmitButton } from '@/components/submit-button' ;
10
10
11
11
import { login , type LoginActionState } from '../actions' ;
12
+ import { useSession } from 'next-auth/react' ;
12
13
13
14
export default function Page ( ) {
14
15
const router = useRouter ( ) ;
@@ -23,6 +24,8 @@ export default function Page() {
23
24
} ,
24
25
) ;
25
26
27
+ const { update : updateSession } = useSession ( ) ;
28
+
26
29
useEffect ( ( ) => {
27
30
if ( state . status === 'failed' ) {
28
31
toast ( {
@@ -36,6 +39,7 @@ export default function Page() {
36
39
} ) ;
37
40
} else if ( state . status === 'success' ) {
38
41
setIsSuccessful ( true ) ;
42
+ updateSession ( ) ;
39
43
router . refresh ( ) ;
40
44
}
41
45
} , [ state . status ] ) ;
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import { SubmitButton } from '@/components/submit-button';
9
9
10
10
import { register , type RegisterActionState } from '../actions' ;
11
11
import { toast } from '@/components/toast' ;
12
+ import { useSession } from 'next-auth/react' ;
12
13
13
14
export default function Page ( ) {
14
15
const router = useRouter ( ) ;
@@ -23,6 +24,8 @@ export default function Page() {
23
24
} ,
24
25
) ;
25
26
27
+ const { update : updateSession } = useSession ( ) ;
28
+
26
29
useEffect ( ( ) => {
27
30
if ( state . status === 'user_exists' ) {
28
31
toast ( { type : 'error' , description : 'Account already exists!' } ) ;
@@ -37,6 +40,7 @@ export default function Page() {
37
40
toast ( { type : 'success' , description : 'Account created successfully!' } ) ;
38
41
39
42
setIsSuccessful ( true ) ;
43
+ updateSession ( ) ;
40
44
router . refresh ( ) ;
41
45
}
42
46
} , [ state ] ) ;
Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ export function SidebarUserNav({ user }: { user: User }) {
43
43
Loading auth status
44
44
</ span >
45
45
</ div >
46
- < div className = "animate-spin text-zinc-500/30 " >
46
+ < div className = "animate-spin text-zinc-500" >
47
47
< LoaderIcon />
48
48
</ div >
49
49
</ SidebarMenuButton >
Original file line number Diff line number Diff line change 1
- import NextAuth from 'next-auth' ;
2
1
import { auth } from './app/(auth)/auth' ;
3
- import { authConfig } from './app/(auth)/auth.config' ;
4
2
import { NextResponse , type NextRequest } from 'next/server' ;
3
+ import { anonymousRegex } from './lib/constants' ;
5
4
6
5
export async function middleware ( request : NextRequest ) {
7
6
// Skip the check for the guest auth endpoint to avoid infinite loops.
@@ -16,12 +15,21 @@ export async function middleware(request: NextRequest) {
16
15
return NextResponse . redirect ( new URL ( '/api/auth/guest' , request . url ) ) ;
17
16
}
18
17
18
+ const isLoggedIn = session . user ;
19
+ const isAnonymousUser = anonymousRegex . test ( session . user ?. email ?? '' ) ;
20
+
21
+ const isOnLoginPage = request . nextUrl . pathname . startsWith ( '/login' ) ;
22
+ const isOnRegisterPage = request . nextUrl . pathname . startsWith ( '/register' ) ;
23
+
24
+ // If the user is logged in and not an anonymous user, redirect to the home page
25
+ if ( isLoggedIn && ! isAnonymousUser && ( isOnLoginPage || isOnRegisterPage ) ) {
26
+ return NextResponse . redirect ( new URL ( '/' , request . url ) ) ;
27
+ }
28
+
19
29
// Otherwise, continue handling the request.
20
30
return NextResponse . next ( ) ;
21
31
}
22
32
23
- export default NextAuth ( authConfig ) . auth ;
24
-
25
33
export const config = {
26
34
matcher : [
27
35
'/' ,
You can’t perform that action at this time.
0 commit comments