-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmiddleware.ts
More file actions
31 lines (24 loc) · 1.06 KB
/
middleware.ts
File metadata and controls
31 lines (24 loc) · 1.06 KB
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
import { NextRequest, NextResponse } from 'next/server';
const PROTECTED_ROUTES = ['/dashboard'];
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Protect dashboard routes: redirect to sign-in if no auth hint cookie
const isProtected = PROTECTED_ROUTES.some((route) => pathname.startsWith(route));
if (isProtected) {
const authHint = request.cookies.get('msal-auth-hint');
if (!authHint?.value) {
const signinUrl = new URL('/auth/signin', request.url);
signinUrl.searchParams.set('callbackUrl', pathname);
return NextResponse.redirect(signinUrl);
}
}
const response = NextResponse.next();
response.headers.set('X-Content-Type-Options', 'nosniff');
response.headers.set('X-Frame-Options', 'SAMEORIGIN');
response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');
response.headers.set('Permissions-Policy', 'camera=(), microphone=(), geolocation=()');
return response;
}
export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico|icons/).*)'],
};