Ping / dist /middlewares /api-key-authentication.js
understanding's picture
Upload 41 files
ba5c923 verified
raw
history blame contribute delete
648 Bytes
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const authentication = (req, res, next) => {
const authHeader = req.headers["authorization"];
if (!authHeader) {
return res.status(401).json({ message: "Missing authorization header" });
}
const apiKey = authHeader.split("Bearer ").at(-1);
if (!apiKey) {
return res.status(401).json({ message: "Invalid authorization format" });
}
if (process.env.API_KEY && apiKey === process.env.API_KEY) {
next();
}
else {
res.status(401).json({ message: "Unauthorized" });
}
};
exports.default = authentication;