Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const basicAuth = require('basic-auth');
- var auth = function(req, res, next) {
- console.log('auth function');
- function unauthorized(res) {
- res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
- return res.send(401);
- };
- const user = basicAuth(req); //Incoming form with name and pass properties
- if (!user || !user.name || !user.pass) {
- return unauthorized(res);
- }
- // authentication properties
- const name = "foo";
- const passcode = "123456";
- if (user.name === name && user.pass === passcode) {
- return next();
- }
- return unauthorized(res);
- };
- app.get('/protectedfile', auth, async function(req, res, next) {
- try {
- res.sendfile('./public/boo.html');
- } catch (error) {
- next(error);
- }
- });
Add Comment
Please, Sign In to add comment