refactor(app.js): Move passport serialize and deserialize to auth module

This commit is contained in:
BoHong Li 2017-04-12 06:07:19 +08:00 committed by Raccoon Li
parent 7ef17fd4e6
commit d88502e331
2 changed files with 22 additions and 19 deletions

19
app.js
View file

@ -154,25 +154,6 @@ app.use(flash())
app.use(passport.initialize()) app.use(passport.initialize())
app.use(passport.session()) app.use(passport.session())
// 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)
})
})
// check uri is valid before going further // check uri is valid before going further
app.use(require('./lib/web/middleware/checkURiValid')) app.use(require('./lib/web/middleware/checkURiValid'))

View file

@ -1,12 +1,34 @@
'use strict' 'use strict'
const Router = require('express').Router const Router = require('express').Router
const passport = require('passport')
const config = require('../../config') const config = require('../../config')
const logger = require('../../logger') const logger = require('../../logger')
const models = require('../../models')
const authRouter = module.exports = Router() 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.facebook) authRouter.use('/', require('./facebook')) if (config.facebook) authRouter.use('/', require('./facebook'))
if (config.twitter) authRouter.use('/', require('./twitter')) if (config.twitter) authRouter.use('/', require('./twitter'))
if (config.github) authRouter.use('/', require('./github')) if (config.github) authRouter.use('/', require('./github'))