Lägg till autentisering och loggning, refaktorisera uppgifter till router

This commit is contained in:
2025-01-24 21:26:06 +01:00
parent 70365715fe
commit e091da93e9
7 changed files with 213 additions and 94 deletions

16
middleware/auth.js Normal file
View File

@@ -0,0 +1,16 @@
const basicAuth = require('basic-auth');
const auth = (req, res, next) => {
const user = basicAuth(req);
const username = process.env.AUTH_USERNAME; // Use environment variables
const password = process.env.AUTH_PASSWORD; // Use environment variables
if (user && user.name === username && user.pass === password) {
return next();
} else {
res.set('WWW-Authenticate', 'Basic realm="401"');
return res.status(401).send('Authentication required.');
}
};
module.exports = auth;