2017-04-11 21:41:14 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
2017-04-11 22:07:19 +00:00
|
|
|
const passport = require('passport')
|
2017-04-11 21:41:14 +00:00
|
|
|
|
|
|
|
const config = require('../../config')
|
|
|
|
const logger = require('../../logger')
|
2017-04-11 22:07:19 +00:00
|
|
|
const models = require('../../models')
|
2017-04-11 21:41:14 +00:00
|
|
|
|
|
|
|
const authRouter = module.exports = Router()
|
|
|
|
|
2017-04-11 22:07:19 +00:00
|
|
|
// serialize and deserialize
|
|
|
|
passport.serializeUser(function (user, done) {
|
|
|
|
logger.info('serializeUser: ' + user.id)
|
|
|
|
return done(null, user.id)
|
|
|
|
})
|
|
|
|
|
|
|
|
passport.deserializeUser(function (id, done) {
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
|
|
|
logger.info('deserializeUser: ' + user.id)
|
|
|
|
return done(null, user)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return done(err, null)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-04-11 22:10:24 +00:00
|
|
|
if (config.facebook) authRouter.use(require('./facebook'))
|
|
|
|
if (config.twitter) authRouter.use(require('./twitter'))
|
|
|
|
if (config.github) authRouter.use(require('./github'))
|
|
|
|
if (config.gitlab) authRouter.use(require('./gitlab'))
|
|
|
|
if (config.dropbox) authRouter.use(require('./dropbox'))
|
|
|
|
if (config.google) authRouter.use(require('./google'))
|
|
|
|
if (config.ldap) authRouter.use(require('./ldap'))
|
|
|
|
if (config.email) authRouter.use(require('./email'))
|
2017-04-11 21:41:14 +00:00
|
|
|
|
|
|
|
// logout
|
|
|
|
authRouter.get('/logout', function (req, res) {
|
|
|
|
if (config.debug && req.isAuthenticated()) {
|
|
|
|
logger.debug('user logout: ' + req.user.id)
|
|
|
|
}
|
|
|
|
req.logout()
|
|
|
|
res.redirect(config.serverurl + '/')
|
|
|
|
})
|