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.

84 lines
2.7 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/github', passport.authenticate('github', { scope: ['user:email'] }))
  60. router.get('/login/slack', passport.authenticate('slack', { scope: ['identity.basic', 'identity.email'] }))
  61. router.get('/login/ms/callback', passport.authenticate('windowslive', { failureRedirect: '/login', successRedirect: '/' }))
  62. router.get('/login/google/callback', passport.authenticate('google', { failureRedirect: '/login', successRedirect: '/' }))
  63. router.get('/login/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login', successRedirect: '/' }))
  64. router.get('/login/github/callback', passport.authenticate('github', { failureRedirect: '/login', successRedirect: '/' }))
  65. router.get('/login/slack/callback', passport.authenticate('slack', { failureRedirect: '/login', successRedirect: '/' }))
  66. /**
  67. * Logout
  68. */
  69. router.get('/logout', function (req, res) {
  70. req.logout()
  71. res.redirect('/')
  72. })
  73. module.exports = router