31 lines
696 B
JavaScript
31 lines
696 B
JavaScript
const config = require('./config');
|
|
|
|
function authenticateApiKey(req, res, next) {
|
|
// If no API key is configured, skip authentication
|
|
if (!config.API_KEY) {
|
|
return next();
|
|
}
|
|
|
|
const providedKey = req.headers['x-api-key'];
|
|
|
|
if (!providedKey) {
|
|
return res.status(401).json({
|
|
error: 'Missing API key',
|
|
message: 'Please provide an API key in the x-api-key header',
|
|
});
|
|
}
|
|
|
|
if (providedKey !== config.API_KEY) {
|
|
return res.status(403).json({
|
|
error: 'Invalid API key',
|
|
message: 'The provided API key is not valid',
|
|
});
|
|
}
|
|
|
|
next();
|
|
}
|
|
|
|
module.exports = {
|
|
authenticateApiKey,
|
|
};
|