-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathmiddleware.ts
38 lines (33 loc) · 1.14 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { NextResponse, type NextRequest } from 'next/server';
import { getInjection } from '@/di/container';
import { SESSION_COOKIE } from '@/config';
export async function middleware(request: NextRequest) {
const isAuthPath =
request.nextUrl.pathname === '/sign-in' ||
request.nextUrl.pathname === '/sign-up';
if (!isAuthPath) {
const sessionId = request.cookies.get(SESSION_COOKIE)?.value;
if (!sessionId) {
return NextResponse.redirect(new URL('/sign-in', request.url));
}
try {
const authenticationService = getInjection('IAuthenticationService');
await authenticationService.validateSession(sessionId);
} catch (err) {
return NextResponse.redirect(new URL('/sign-in', request.url));
}
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico, sitemap.xml, robots.txt (metadata files)
*/
'/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
],
};