Guest User

Untitled

a guest
Sep 24th, 2018
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. const basicAuth = require('basic-auth');
  2. var auth = function(req, res, next) {
  3. console.log('auth function');
  4. function unauthorized(res) {
  5. res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
  6. return res.send(401);
  7. };
  8.  
  9. const user = basicAuth(req); //Incoming form with name and pass properties
  10. if (!user || !user.name || !user.pass) {
  11. return unauthorized(res);
  12. }
  13.  
  14. // authentication properties
  15. const name = "foo";
  16. const passcode = "123456";
  17. if (user.name === name && user.pass === passcode) {
  18. return next();
  19. }
  20. return unauthorized(res);
  21. };
  22.  
  23. app.get('/protectedfile', auth, async function(req, res, next) {
  24. try {
  25. res.sendfile('./public/boo.html');
  26. } catch (error) {
  27. next(error);
  28. }
  29. });
Add Comment
Please, Sign In to add comment