adding check on bearer

This commit is contained in:
Carlos
2025-04-10 00:30:49 -04:00
parent 6c10c6e8fe
commit 267c90eca6

View File

@ -12,12 +12,21 @@ app.use(bodyParser.json());
// API Key validation middleware
const validateApiKey = (req, res, next) => {
const apiKey = req.headers["api-key"];
const authHeader = req.headers["authorization"];
if (!apiKey) {
// Try to extract token from Authorization: Bearer <token>
let token = null;
if (authHeader && authHeader.startsWith("Bearer ")) {
token = authHeader.split(" ")[1];
}
const providedKey = apiKeyHeader || token;
if (!providedKey) {
return res.status(400).json({ error: "API key is missing" });
}
if (apiKey !== process.env.API_KEY) {
if (providedKey !== process.env.API_KEY) {
return res.status(403).json({ error: "Invalid API key" });
}