-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
31 lines (22 loc) · 776 Bytes
/
middleware.ts
File metadata and controls
31 lines (22 loc) · 776 Bytes
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 { NextResponse, NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname;
if (request.method === 'OPTIONS') {
return NextResponse.next();
}
if (pathname.startsWith('/api/auth/') || (pathname.startsWith('/api/comments') && request.method === 'GET')) {
return NextResponse.next();
}
const requiresAuth = pathname.startsWith('/api/profile') || pathname.startsWith('/api/comments');
if (!requiresAuth) {
return NextResponse.next();
}
const token = request.cookies.get('jwt')?.value;
if (!token) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
return NextResponse.next();
}
export const config = {
matcher: ['/api/:path*'],
};