N-FIN-79: add auth0 sdk

This commit is contained in:
Markus Thielker 2024-04-04 00:22:43 +02:00
parent 39cd91a53a
commit 642d64ad5e
No known key found for this signature in database
4 changed files with 152 additions and 31 deletions

View file

@ -0,0 +1,3 @@
import { handleAuth } from '@auth0/nextjs-auth0';
export const GET = handleAuth();

View file

@ -1,27 +1,3 @@
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { URL_AUTH, URL_HOME, URL_SIGN_IN } from './lib/constants';
import { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/edge';
export async function middleware(request: NextRequest) {
// get session id from cookies
const sessionId = request.cookies.get('auth_session')?.value ?? null;
// redirect to home if user is already authenticated
if (request.nextUrl.pathname.startsWith(URL_AUTH) && sessionId) {
return NextResponse.redirect(new URL(URL_HOME, request.url));
}
// redirect to sign in if user is not authenticated
if (!request.nextUrl.pathname.startsWith(URL_AUTH) && !sessionId) {
return NextResponse.redirect(new URL(URL_SIGN_IN, request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
};
export default withMiddlewareAuthRequired();