adding check on bearer
This commit is contained in:
13
server.js
13
server.js
@ -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" });
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user