File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * Configuration for dashboard routes that require authentication.
3+ *
4+ * To add a new protected route, simply add the path to this array.
5+ * To remove protection from a route, remove it from this array.
6+ *
7+ * Routes are matched using prefix matching, so nested routes under
8+ * a protected path will also be protected (e.g., /dashboard/projects/123
9+ * is protected if /dashboard/projects is in this array).
10+ */
11+ export const PROTECTED_DASHBOARD_ROUTES = [
12+ '/dashboard/projects' ,
13+ '/dashboard/sheet' ,
14+ ] as const ;
15+
Original file line number Diff line number Diff line change 11import { getToken } from "next-auth/jwt" ;
22import { NextRequest , NextResponse } from "next/server" ;
3+ import { PROTECTED_DASHBOARD_ROUTES } from "@/lib/auth/protected-routes" ;
34
45export async function middleware ( req : NextRequest ) {
56 const adaptedReq = {
@@ -10,12 +11,18 @@ export async function middleware(req: NextRequest) {
1011 req : adaptedReq as any ,
1112 secret : process . env . NEXTAUTH_SECRET ,
1213 } ) ;
13- const protectedPaths = [ "/dashboard" ] ;
14- if ( protectedPaths . some ( ( path ) => req . nextUrl . pathname . startsWith ( path ) ) ) {
15- if ( ! token ) {
16- const signInUrl = new URL ( "/login" , req . url ) ;
17- return NextResponse . redirect ( signInUrl ) ;
18- }
14+
15+ const pathname = req . nextUrl . pathname ;
16+
17+ const isProtectedRoute = PROTECTED_DASHBOARD_ROUTES . some ( ( path ) =>
18+ pathname . startsWith ( path )
19+ ) ;
20+
21+ if ( isProtectedRoute && ! token ) {
22+ const signInUrl = new URL ( "/login" , req . url ) ;
23+ signInUrl . searchParams . set ( "callbackUrl" , pathname ) ;
24+ return NextResponse . redirect ( signInUrl ) ;
1925 }
26+
2027 return NextResponse . next ( ) ;
2128}
You can’t perform that action at this time.
0 commit comments