forked from typetale-app/mailgun-ses-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
36 lines (31 loc) · 1.07 KB
/
Copy pathproxy.ts
File metadata and controls
36 lines (31 loc) · 1.07 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { authentication } from "./lib/authentication";
import logger from "./lib/core/logger";
/**
* Middleware to check for a valid API key in the request headers
* @param request - The incoming request
* @returns NextResponse - The response object
*/
const whitelist = [
"/healthcheck",
]
const log = logger.child({ path: "middleware" })
export async function proxy(request: NextRequest) {
if (whitelist.some((path) => request.nextUrl.pathname.startsWith(path))) {
return NextResponse.next();
}
const token = request.headers.get("authorization")
if (token) {
const result = await authentication(token)
if (result) {
return NextResponse.next();
}
}
log.error({ path: request.nextUrl.pathname }, "API key not found")
return Response.json({ error: 'authentication failed' }, { status: 401 })
}
// Match all routes
export const config = {
matcher: "/:path((?!.*\\.(?:css|js|png|jpg|jpeg|gif|webp|svg|ico)).*)",
}