File tree Expand file tree Collapse file tree 6 files changed +23
-31
lines changed
Expand file tree Collapse file tree 6 files changed +23
-31
lines changed Original file line number Diff line number Diff line change 1- import { anonymousRegex } from '@/lib/constants' ;
21import type { NextAuthConfig } from 'next-auth' ;
32
43export const authConfig = {
@@ -10,29 +9,5 @@ export const authConfig = {
109 // added later in auth.ts since it requires bcrypt which is only compatible with Node.js
1110 // while this file is also used in non-Node.js environments
1211 ] ,
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 : { } ,
3813} satisfies NextAuthConfig ;
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ export const {
2929 const passwordsMatch = await compare ( password , user . password ) ;
3030
3131 if ( ! passwordsMatch ) return null ;
32+
3233 return user ;
3334 } ,
3435 } ) ,
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import { AuthForm } from '@/components/auth-form';
99import { SubmitButton } from '@/components/submit-button' ;
1010
1111import { login , type LoginActionState } from '../actions' ;
12+ import { useSession } from 'next-auth/react' ;
1213
1314export default function Page ( ) {
1415 const router = useRouter ( ) ;
@@ -23,6 +24,8 @@ export default function Page() {
2324 } ,
2425 ) ;
2526
27+ const { update : updateSession } = useSession ( ) ;
28+
2629 useEffect ( ( ) => {
2730 if ( state . status === 'failed' ) {
2831 toast ( {
@@ -36,6 +39,7 @@ export default function Page() {
3639 } ) ;
3740 } else if ( state . status === 'success' ) {
3841 setIsSuccessful ( true ) ;
42+ updateSession ( ) ;
3943 router . refresh ( ) ;
4044 }
4145 } , [ state . status ] ) ;
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import { SubmitButton } from '@/components/submit-button';
99
1010import { register , type RegisterActionState } from '../actions' ;
1111import { toast } from '@/components/toast' ;
12+ import { useSession } from 'next-auth/react' ;
1213
1314export default function Page ( ) {
1415 const router = useRouter ( ) ;
@@ -23,6 +24,8 @@ export default function Page() {
2324 } ,
2425 ) ;
2526
27+ const { update : updateSession } = useSession ( ) ;
28+
2629 useEffect ( ( ) => {
2730 if ( state . status === 'user_exists' ) {
2831 toast ( { type : 'error' , description : 'Account already exists!' } ) ;
@@ -37,6 +40,7 @@ export default function Page() {
3740 toast ( { type : 'success' , description : 'Account created successfully!' } ) ;
3841
3942 setIsSuccessful ( true ) ;
43+ updateSession ( ) ;
4044 router . refresh ( ) ;
4145 }
4246 } , [ state ] ) ;
Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ export function SidebarUserNav({ user }: { user: User }) {
4343 Loading auth status
4444 </ span >
4545 </ div >
46- < div className = "animate-spin text-zinc-500/30 " >
46+ < div className = "animate-spin text-zinc-500" >
4747 < LoaderIcon />
4848 </ div >
4949 </ SidebarMenuButton >
Original file line number Diff line number Diff line change 1- import NextAuth from 'next-auth' ;
21import { auth } from './app/(auth)/auth' ;
3- import { authConfig } from './app/(auth)/auth.config' ;
42import { NextResponse , type NextRequest } from 'next/server' ;
3+ import { anonymousRegex } from './lib/constants' ;
54
65export async function middleware ( request : NextRequest ) {
76 // Skip the check for the guest auth endpoint to avoid infinite loops.
@@ -16,12 +15,21 @@ export async function middleware(request: NextRequest) {
1615 return NextResponse . redirect ( new URL ( '/api/auth/guest' , request . url ) ) ;
1716 }
1817
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+
1929 // Otherwise, continue handling the request.
2030 return NextResponse . next ( ) ;
2131}
2232
23- export default NextAuth ( authConfig ) . auth ;
24-
2533export const config = {
2634 matcher : [
2735 '/' ,
You can’t perform that action at this time.
0 commit comments