forked from balahero03/eOrbitor_Pulse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
47 lines (39 loc) · 1.23 KB
/
Copy pathproxy.ts
File metadata and controls
47 lines (39 loc) · 1.23 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
36
37
38
39
40
41
42
43
44
45
46
47
import { NextRequest, NextResponse } from 'next/server';
const EXEMPT_PATHS = [
'/api/auth/login',
'/api/auth/me',
'/api/access-status',
'/api/access-requests',
'/api/notifications',
'/api/time-tracking',
'/api/cron/inactive-users',
];
function isExemptPath(pathname: string): boolean {
return EXEMPT_PATHS.some((p) => pathname === p || pathname.startsWith(p + '/'));
}
export const config = { matcher: ['/api/:path*'] };
export async function proxy(req: NextRequest) {
const { pathname } = req.nextUrl;
if (isExemptPath(pathname)) return NextResponse.next();
const authHeader = req.headers.get('authorization');
if (!authHeader?.startsWith('Bearer ')) return NextResponse.next();
try {
const res = await fetch(new URL('/api/access-status', req.nextUrl.origin), {
headers: {
'authorization': authHeader,
},
});
if (res.ok) {
const gate = await res.json();
if (gate && gate.blocked) {
return NextResponse.json(
{ message: 'Access restricted outside allowed hours', code: 'ACCESS_RESTRICTED', ...gate },
{ status: 403 }
);
}
}
} catch (err) {
console.error('Access check failed in proxy:', err);
}
return NextResponse.next();
}