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.

80 lines
2.3 KiB

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