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.

71 lines
1.6 KiB

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