added stuff to middleware

This commit is contained in:
Ubuntu 2025-03-27 19:14:36 +00:00
parent 7d9148157f
commit c9023ea862

View file

@ -1,29 +1,32 @@
import { auth0 } from './lib/auth0'; import { NextRequest, NextResponse } from "next/server"
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { auth0 } from "@/lib/auth0"
export async function middleware(request: NextRequest) { export async function middleware(request: NextRequest) {
const res = await auth0.getSession(request); const authRes = await auth0.middleware(request)
if (!res) { if (request.nextUrl.pathname.startsWith("/auth")) {
// not logged in, redirect to Auth0 login return authRes
return Response.redirect(
new URL('/auth/login', request.url),
302
);
} }
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))
} }
export const config = { // the headers from the auth middleware should always be returned
matcher: [ return authRes
/* }
* Match all paths except:
* - public files (_next, images, icons, etc.) // export const config = {
* - auth routes like /auth/login and /auth/callback // matcher: [
*/ // /*
'/((?!_next/static|_next/image|favicon.ico|auth/.*).*)', // * 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/.*).*)',
// ],
// };