1
- import { NextRequest , NextResponse } from "next/server" ;
2
- import { auth0 } from "./lib/auth0" ;
1
+ import type { NextRequest } from "next/server"
3
2
4
- const APP_SESSION_COOKIE = "appSession" ;
3
+ import { auth0 } from "./lib/auth0"
5
4
6
5
export async function middleware ( request : NextRequest ) {
7
- const { pathname } = request . nextUrl ;
8
-
9
- if ( pathname . startsWith ( "/auth" ) ) {
10
- return auth0 . middleware ( request ) ;
11
- }
12
-
13
- try {
14
- const session = await auth0 . getSession ( request ) ;
15
- if ( ! session ) {
16
- return NextResponse . redirect ( new URL ( "/auth/login" , request . url ) ) ;
17
- }
18
- return NextResponse . next ( ) ;
19
- } catch {
20
- const redirect = NextResponse . redirect ( new URL ( "/auth/login" , request . url ) ) ;
21
- redirect . cookies . set ( APP_SESSION_COOKIE , "" , {
22
- path : "/" ,
23
- httpOnly : true ,
24
- maxAge : 0 ,
25
- } ) ;
26
- return redirect ;
27
- }
6
+ return await auth0 . middleware ( request ) ;
28
7
}
29
8
30
9
export const config = {
31
10
matcher : [
32
- "/((?!_next/static|_next/image|images|favicon\\.(?:ico|png)|sitemap\\.xml|robots\\.txt|$).*)" ,
11
+ /*
12
+ * Match all request paths except for the ones starting with:
13
+ * - _next/static (static files)
14
+ * - _next/image (image optimization files)
15
+ * - favicon.ico, sitemap.xml, robots.txt (metadata files)
16
+ */
17
+ "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)" ,
33
18
] ,
34
- } ;
19
+ }
0 commit comments