@@ -4,53 +4,50 @@ import { headers } from "next/headers";
44import { NextRequest , NextResponse } from "next/server" ;
55
66export const config = {
7- runtime : "nodejs" ,
8- matcher : [
9- // Skip auth-related routes
10- "/((?!api|_next/static|_next/image|favicon.ico|monitoring|ingest|onboarding|research).*)" ,
11- ] ,
7+ runtime : "nodejs" ,
8+ matcher : [
9+ // Skip auth-related routes
10+ "/((?!api|_next/static|_next/image|favicon.ico|monitoring|ingest|onboarding|research).*)" ,
11+ ] ,
1212} ;
1313
1414export async function middleware ( request : NextRequest ) {
15- const session = await auth . api . getSession ( {
16- headers : await headers ( ) ,
17- } ) ;
18-
19- const response = NextResponse . next ( ) ;
20- const nextUrl = request . nextUrl ;
21-
22- // Add x-path-name
23- response . headers . set ( "x-pathname" , nextUrl . pathname ) ;
24-
25- // 1. Not authenticated
26- if ( ! session && nextUrl . pathname !== "/auth" ) {
27- const url = new URL ( "/auth" , request . url ) ;
28-
29- return NextResponse . redirect ( url ) ;
30- }
31-
32- // 2. Authenticated; redirect to onboarding if not completed
33- if ( session ) {
34- // 2.1. If the user has an active organization, redirect to implementation
35- if (
36- session . session . activeOrganizationId &&
37- nextUrl . pathname !== "/auth" &&
38- ! nextUrl . pathname . startsWith ( "/setup/onboarding" )
39- ) {
40- const onboarding = await db . onboarding . findFirst ( {
41- where : {
42- organizationId : session . session
43- . activeOrganizationId as string ,
44- } ,
45- } ) ;
46-
47- if ( ! onboarding ?. completed && ! onboarding ?. triggerJobId ) {
48- return NextResponse . redirect (
49- new URL ( "/setup/onboarding" , request . url ) ,
50- ) ;
51- }
52- }
53- }
54-
55- return response ;
15+ const session = await auth . api . getSession ( {
16+ headers : await headers ( ) ,
17+ } ) ;
18+
19+ const response = NextResponse . next ( ) ;
20+ const nextUrl = request . nextUrl ;
21+
22+ // Add x-path-name
23+ response . headers . set ( "x-pathname" , nextUrl . pathname ) ;
24+
25+ // 1. Not authenticated
26+ if ( ! session && nextUrl . pathname !== "/auth" ) {
27+ const url = new URL ( "/auth" , request . url ) ;
28+
29+ return NextResponse . redirect ( url ) ;
30+ }
31+
32+ // 2. Authenticated; redirect to onboarding if not completed
33+ if ( session ) {
34+ // 2.1. If the user has an active organization, redirect to implementation
35+ if (
36+ session . session . activeOrganizationId &&
37+ nextUrl . pathname !== "/auth" &&
38+ ! nextUrl . pathname . startsWith ( "/setup/onboarding" )
39+ ) {
40+ const onboarding = await db . onboarding . findFirst ( {
41+ where : {
42+ organizationId : session . session . activeOrganizationId as string ,
43+ } ,
44+ } ) ;
45+
46+ if ( onboarding && ! onboarding . completed && ! onboarding . triggerJobId ) {
47+ return NextResponse . redirect ( new URL ( "/setup/onboarding" , request . url ) ) ;
48+ }
49+ }
50+ }
51+
52+ return response ;
5653}
0 commit comments