summaryrefslogtreecommitdiff
path: root/lib/web/auth/index.js
diff options
context:
space:
mode:
authorMax Wu2017-05-14 17:50:25 +0800
committerGitHub2017-05-14 17:50:25 +0800
commitea9b1557f9834b2b140bd9b07488b5fca3953791 (patch)
tree8e96eb37c1715f860a698d5e7eeb2381b00af9d8 /lib/web/auth/index.js
parentf85d1d880114b6787fdb2bac488341776deaad3d (diff)
parentc37b6669158750ab2222e81a5a8fc3a5c8c353f5 (diff)
Merge pull request #421 from hackmdio/BackendRefactor
Backend code refactoring - part 1
Diffstat (limited to 'lib/web/auth/index.js')
-rw-r--r--lib/web/auth/index.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/web/auth/index.js b/lib/web/auth/index.js
new file mode 100644
index 00000000..b5ca8434
--- /dev/null
+++ b/lib/web/auth/index.js
@@ -0,0 +1,48 @@
+'use strict'
+
+const Router = require('express').Router
+const passport = require('passport')
+
+const config = require('../../config')
+const logger = require('../../logger')
+const models = require('../../models')
+
+const authRouter = module.exports = Router()
+
+// 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)
+ })
+})
+
+if (config.isFacebookEnable) authRouter.use(require('./facebook'))
+if (config.isTwitterEnable) authRouter.use(require('./twitter'))
+if (config.isGitHubEnable) authRouter.use(require('./github'))
+if (config.isGitLabEnable) authRouter.use(require('./gitlab'))
+if (config.isDropboxEnable) authRouter.use(require('./dropbox'))
+if (config.isGoogleEnable) authRouter.use(require('./google'))
+if (config.isLDAPEnable) authRouter.use(require('./ldap'))
+if (config.isEmailEnable) authRouter.use(require('./email'))
+
+// 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 + '/')
+})