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 { 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))
|
||||||
|
}
|
||||||
|
|
||||||
|
// the headers from the auth middleware should always be returned
|
||||||
|
return authRes
|
||||||
}
|
}
|
||||||
|
|
||||||
export const config = {
|
// export const config = {
|
||||||
matcher: [
|
// matcher: [
|
||||||
/*
|
// /*
|
||||||
* Match all paths except:
|
// * Match all paths except:
|
||||||
* - public files (_next, images, icons, etc.)
|
// * - public files (_next, images, icons, etc.)
|
||||||
* - auth routes like /auth/login and /auth/callback
|
// * - auth routes like /auth/login and /auth/callback
|
||||||
*/
|
// */
|
||||||
'/((?!_next/static|_next/image|favicon.ico|auth/.*).*)',
|
// '/((?!_next/static|_next/image|favicon.ico|auth/.*).*)',
|
||||||
],
|
// ],
|
||||||
};
|
// };
|
||||||
Loading…
Add table
Add a link
Reference in a new issue