-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
35 lines (28 loc) · 1.17 KB
/
middleware.ts
File metadata and controls
35 lines (28 loc) · 1.17 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
32
33
34
35
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
// Check for token in both cookies and localStorage (via headers)
const token = request.cookies.get('accessToken')?.value ||
request.headers.get('authorization')?.replace('Bearer ', '');
const isAuthPage = request.nextUrl.pathname.startsWith('/auth');
const isExamPage = request.nextUrl.pathname.startsWith('/exam');
const isApiRoute = request.nextUrl.pathname.startsWith('/api');
// Skip middleware for API routes
if (isApiRoute) {
return NextResponse.next();
}
// If user is not authenticated and trying to access protected routes
if (!token && (isExamPage || request.nextUrl.pathname === '/results')) {
return NextResponse.redirect(new URL('/auth/login', request.url));
}
// If user is authenticated and trying to access auth pages
if (token && isAuthPage) {
return NextResponse.redirect(new URL('/', request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
};