done
This commit is contained in:
23
Server/middleware/authMiddleware.js
Normal file
23
Server/middleware/authMiddleware.js
Normal file
@ -0,0 +1,23 @@
|
||||
function basicAuth(req, res, next) {
|
||||
const authHeader = req.headers['authorization'];
|
||||
|
||||
if (!authHeader || !authHeader.startsWith('Basic ')) {
|
||||
res.setHeader('WWW-Authenticate', 'Basic');
|
||||
return res.status(401).send('Authentication required.');
|
||||
}
|
||||
|
||||
const base64Credentials = authHeader.split(' ')[1];
|
||||
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii');
|
||||
const [username, password] = credentials.split(':');
|
||||
|
||||
const USERNAME = 'rahasia';
|
||||
const PASSWORD = 'tomat';
|
||||
|
||||
if (username === USERNAME && password === PASSWORD) {
|
||||
next();
|
||||
} else {
|
||||
return res.status(403).send('Access denied.');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = basicAuth;
|
Reference in New Issue
Block a user