added stuff to middleware
This commit is contained in:
parent
7d9148157f
commit
c9023ea862
1 changed files with 24 additions and 21 deletions
|
|
@ -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))
|
||||
}
|
||||
|
||||
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/.*).*)',
|
||||
],
|
||||
};
|
||||
// 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/.*).*)',
|
||||
// ],
|
||||
// };
|
||||
Loading…
Add table
Add a link
Reference in a new issue