You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.6 KiB

  1. var express = require('express');
  2. var router = express.Router();
  3. var passport = require('passport');
  4. var ExpressBrute = require('express-brute');
  5. var ExpressBruteRedisStore = require('express-brute-redis');
  6. var moment = require('moment');
  7. /**
  8. * Setup Express-Brute
  9. */
  10. var EBstore = new ExpressBruteRedisStore({
  11. prefix: 'bf:',
  12. client: red
  13. });
  14. var bruteforce = new ExpressBrute(EBstore, {
  15. freeRetries: 5,
  16. minWait: 60 * 1000,
  17. maxWait: 5 * 60 * 1000,
  18. refreshTimeoutOnRequest: false,
  19. failCallback(req, res, next, nextValidRequestDate) {
  20. req.flash('alert', {
  21. class: 'error',
  22. title: 'Too many attempts!',
  23. message: "You've made too many failed attempts in a short period of time, please try again " + moment(nextValidRequestDate).fromNow() + '.',
  24. iconClass: 'fa-times'
  25. });
  26. res.redirect('/login');
  27. }
  28. });
  29. /**
  30. * Login form
  31. */
  32. router.get('/login', function(req, res, next) {
  33. res.render('auth/login', {
  34. usr: res.locals.usr
  35. });
  36. });
  37. router.post('/login', bruteforce.prevent, function(req, res, next) {
  38. passport.authenticate('local', function(err, user, info) {
  39. if (err) { return next(err); }
  40. if (!user) {
  41. req.flash('alert', {
  42. class: 'error',
  43. title: 'Invalid login',
  44. message: "The email or password is invalid.",
  45. iconClass: 'fa-times'
  46. });
  47. return res.redirect('/login');
  48. }
  49. req.logIn(user, function(err) {
  50. if (err) { return next(err); }
  51. req.brute.reset(function () {
  52. return res.redirect('/');
  53. });
  54. });
  55. })(req, res, next);
  56. });
  57. /**
  58. * Logout
  59. */
  60. router.get('/logout', function(req, res) {
  61. req.logout();
  62. res.redirect('/');
  63. });
  64. module.exports = router;