forked from Hermanoid/GTNH-AE2-OC-GOG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
46 lines (39 loc) · 1.43 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
39
40
41
42
43
44
45
46
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { updateSession } from "@/util/supabase/middleware";
export async function middleware(request: NextRequest) {
const url = request.nextUrl;
if (url.pathname.startsWith('/api/cron')) {
const authHeader = request.headers.get('authorization');
const token = request.headers.get('x-secret');
if (authHeader !== `Bearer ${process.env.CRON_SECRET}` && !token) {
return new Response('Unauthorized', {
status: 401,
});
}
}
// Only apply this middleware to /api routes
if (url.pathname.startsWith('/api')) {
const token = request.headers.get('x-secret');
if (token !== process.env.SECRET) {
return NextResponse.json(
{ message: 'Unauthorized' },
{ status: 401 }
);
}
}
return await updateSession(request);
}
export const config = {
matcher: [
/*
* Match all request paths except:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - images - .svg, .png, .jpg, .jpeg, .gif, .webp
* Feel free to modify this pattern to include more paths.
*/
"/((?!_next/static|_next/image|favicon.svg|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};