wikijs-fork/controllers/auth.js

85 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-02-09 01:52:37 +00:00
'use strict'
const express = require('express')
const router = express.Router()
const passport = require('passport')
const ExpressBrute = require('express-brute')
const ExpressBruteMongooseStore = require('express-brute-mongoose')
const moment = require('moment')
2016-08-17 03:56:08 +00:00
/**
* Setup Express-Brute
*/
2017-02-09 01:52:37 +00:00
const EBstore = new ExpressBruteMongooseStore(db.Bruteforce)
const bruteforce = new ExpressBrute(EBstore, {
freeRetries: 5,
minWait: 60 * 1000,
maxWait: 5 * 60 * 1000,
refreshTimeoutOnRequest: false,
failCallback (req, res, next, nextValidRequestDate) {
req.flash('alert', {
class: 'error',
title: 'Too many attempts!',
message: "You've made too many failed attempts in a short period of time, please try again " + moment(nextValidRequestDate).fromNow() + '.',
iconClass: 'fa-times'
})
res.redirect('/login')
}
})
2016-08-17 03:56:08 +00:00
/**
* Login form
*/
2017-02-09 01:52:37 +00:00
router.get('/login', function (req, res, next) {
res.render('auth/login', {
usr: res.locals.usr
})
})
2016-08-17 03:56:08 +00:00
2017-02-09 01:52:37 +00:00
router.post('/login', bruteforce.prevent, function (req, res, next) {
passport.authenticate('local', function (err, user, info) {
if (err) { return next(err) }
2016-08-17 03:56:08 +00:00
2017-02-09 01:52:37 +00:00
if (!user) {
req.flash('alert', {
title: 'Invalid login',
message: 'The email or password is invalid.'
})
return res.redirect('/login')
}
2016-08-17 03:56:08 +00:00
2017-02-09 01:52:37 +00:00
req.logIn(user, function (err) {
if (err) { return next(err) }
2016-10-31 03:03:36 +00:00
req.brute.reset(function () {
2017-02-09 01:52:37 +00:00
return res.redirect('/')
})
})
})(req, res, next)
})
2016-08-17 03:56:08 +00:00
/**
* Social Login
*/
2017-02-09 01:52:37 +00:00
router.get('/login/ms', passport.authenticate('windowslive', { scope: ['wl.signin', 'wl.basic', 'wl.emails'] }))
router.get('/login/google', passport.authenticate('google', { scope: ['profile', 'email'] }))
router.get('/login/facebook', passport.authenticate('facebook', { scope: ['public_profile', 'email'] }))
router.get('/login/github', passport.authenticate('github', { scope: ['user:email'] }))
router.get('/login/slack', passport.authenticate('slack', { scope: ['identity.basic', 'identity.email'] }))
2017-02-09 01:52:37 +00:00
router.get('/login/ms/callback', passport.authenticate('windowslive', { failureRedirect: '/login', successRedirect: '/' }))
router.get('/login/google/callback', passport.authenticate('google', { failureRedirect: '/login', successRedirect: '/' }))
router.get('/login/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login', successRedirect: '/' }))
router.get('/login/github/callback', passport.authenticate('github', { failureRedirect: '/login', successRedirect: '/' }))
router.get('/login/slack/callback', passport.authenticate('slack', { failureRedirect: '/login', successRedirect: '/' }))
2016-08-17 03:56:08 +00:00
/**
* Logout
*/
2017-02-09 01:52:37 +00:00
router.get('/logout', function (req, res) {
req.logout()
res.redirect('/')
})
2016-08-17 03:56:08 +00:00
2017-02-09 01:52:37 +00:00
module.exports = router