You just put this code before doing the routes configuration.
var app = express();
//-----------------------------------
// attach middlewares
//-----------------------------------
app.use(function(req, res, next) {
// define username and password here
var auth = { username: 'username', password: 'password' };
var b64auth = (req.headers.authorization || '').split(' ')[1] || '';
var [username, password] = new Buffer(b64auth, 'base64').toString().split(':');
// Verify username and password are correct
if (!username || !password || username !== auth.username || password !== auth.password) {
res.set('WWW-Authenticate', 'Basic realm="nope"');
return res.status(401).send('Unauthorized');
} else {
return next();
}
});
ไม่มีความคิดเห็น:
แสดงความคิดเห็น