NORY-16: check if session exists in middleware (authentication)
This commit is contained in:
parent
531f0453e7
commit
71df52446c
1 changed files with 34 additions and 0 deletions
34
authentication/src/middleware.ts
Normal file
34
authentication/src/middleware.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getFrontendApi } from '@/ory/sdk/hydra';
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
export async function middleware(request: NextRequest) {
|
||||
|
||||
const api = await getFrontendApi();
|
||||
const cookie = await cookies();
|
||||
|
||||
const session = await api
|
||||
.toSession({ cookie: 'ory_kratos_session=' + cookie.get('ory_kratos_session')?.value })
|
||||
.then((response) => response.data)
|
||||
.catch(() => null);
|
||||
|
||||
if (!session && !request.nextUrl.pathname.startsWith('/flow')) {
|
||||
|
||||
console.log('NO SESSION');
|
||||
|
||||
const url = request.nextUrl.host +
|
||||
'/flow/login?return_to=' +
|
||||
request.nextUrl.host +
|
||||
request.nextUrl.pathname;
|
||||
|
||||
console.log('REDIRECT TO', url);
|
||||
|
||||
return NextResponse.redirect(url);
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: '/((?!api|_next/static|_next/image|favicon.png|sitemap.xml|robots.txt).*)',
|
||||
};
|
Loading…
Add table
Reference in a new issue