🔒 add app access token middleware

Create a middleware app access token, so that all requests must include `access_token` in the header with a value equal
to API_KEY in the .env file. If not, a `403 Forbidden` error will be returned.
This commit is contained in:
2025-08-13 11:26:57 +07:00
parent c1adb767e7
commit 89ebfb8aa4
2 changed files with 17 additions and 1 deletions

View File

@ -0,0 +1,12 @@
import Elysia, { Context } from "elysia";
import { returnErrorResponse } from "../../helpers/callback/httpResponse";
export const appAccessTokenMiddleware = () =>
new Elysia().onRequest(({ request, set }) => {
const headerToken = request.headers.get("access_token");
const storedToken = process.env.API_KEY;
if (headerToken !== storedToken) {
return returnErrorResponse(set, 403, "Unauthorized");
}
});