-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
26 lines (21 loc) · 924 Bytes
/
middleware.ts
File metadata and controls
26 lines (21 loc) · 924 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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
// Check if the request is for the API routes
if (request.nextUrl.pathname.startsWith('/api')) {
// Get the Authorization header
const authHeader = request.headers.get('Authorization');
// If no Authorization header is present, return a 401 Unauthorized response
if (!authHeader) {
return NextResponse.json({ success: false, message: 'Authorization header is required' }, { status: 401 });
}
// You can add additional authorization logic here
// For example, validate JWT tokens, check specific auth schemes, etc.
}
// Continue with the request for non-API routes or if authorization is valid
return NextResponse.next();
}
// Configure the middleware to only run on API routes
export const config = {
matcher: ['/api/:path*'],
};