diff --git a/src/middleware.ts b/src/middleware.ts index 8081c35..d8c0101 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,29 +1,32 @@ -import { auth0 } from './lib/auth0'; -import { NextResponse } from 'next/server'; -import type { NextRequest } from 'next/server'; +import { NextRequest, NextResponse } from "next/server" +import { auth0 } from "@/lib/auth0" export async function middleware(request: NextRequest) { - const res = await auth0.getSession(request); + const authRes = await auth0.middleware(request) - if (!res) { - // not logged in, redirect to Auth0 login - return Response.redirect( - new URL('/auth/login', request.url), - 302 - ); + if (request.nextUrl.pathname.startsWith("/auth")) { + return authRes } - return NextResponse.next(); + const session = await auth0.getSession(request) + + if (!session) { + // user is not authenticated, redirect to login page + return NextResponse.redirect(new URL("/auth/login", request.nextUrl.origin)) + } + + // the headers from the auth middleware should always be returned + return authRes } -export const config = { - matcher: [ - /* - * Match all paths except: - * - public files (_next, images, icons, etc.) - * - auth routes like /auth/login and /auth/callback - */ - '/((?!_next/static|_next/image|favicon.ico|auth/.*).*)', - ], -}; \ No newline at end of file +// export const config = { +// matcher: [ +// /* +// * Match all paths except: +// * - public files (_next, images, icons, etc.) +// * - auth routes like /auth/login and /auth/callback +// */ +// '/((?!_next/static|_next/image|favicon.ico|auth/.*).*)', +// ], +// }; \ No newline at end of file