Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //authentication middleware.
- function auth(req, res, next) {
- console.log(req.headers);
- var authHeader = req.headers.authorization;
- if(!authHeader) {
- var err = new Error('You are not authenticated');
- res.setHeader("WWW-Authenticate", 'Basic');
- err.status = 401;
- next(err);
- }
- var auth = new Buffer(authHeader.split(' ')[1], 'base64').toString().split(':');
- var username = auth[0];
- var password = auth[1];
- if(username === 'admin' && password === 'password') {
- next();
- }
- else {
- var err = new Error('Incorrect username/password');
- res.setHeader("WWW-Authenticate", 'Basic');
- err.status = 401;
- next(err);
- }
- };
- app.use(auth);
Add Comment
Please, Sign In to add comment