2017-04-11 21:41:14 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const models = require('../../models')
|
|
|
|
const logger = require('../../logger')
|
|
|
|
|
|
|
|
exports.setReturnToFromReferer = function setReturnToFromReferer (req) {
|
|
|
|
var referer = req.get('referer')
|
|
|
|
if (!req.session) req.session = {}
|
|
|
|
req.session.returnTo = referer
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.passportGeneralCallback = function callback (accessToken, refreshToken, profile, done) {
|
|
|
|
var stringifiedProfile = JSON.stringify(profile)
|
|
|
|
models.User.findOrCreate({
|
|
|
|
where: {
|
|
|
|
profileid: profile.id.toString()
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
profile: stringifiedProfile,
|
|
|
|
accessToken: accessToken,
|
|
|
|
refreshToken: refreshToken
|
|
|
|
}
|
|
|
|
}).spread(function (user, created) {
|
|
|
|
if (user) {
|
|
|
|
var needSave = false
|
|
|
|
if (user.profile !== stringifiedProfile) {
|
|
|
|
user.profile = stringifiedProfile
|
|
|
|
needSave = true
|
|
|
|
}
|
|
|
|
if (user.accessToken !== accessToken) {
|
|
|
|
user.accessToken = accessToken
|
|
|
|
needSave = true
|
|
|
|
}
|
|
|
|
if (user.refreshToken !== refreshToken) {
|
|
|
|
user.refreshToken = refreshToken
|
|
|
|
needSave = true
|
|
|
|
}
|
|
|
|
if (needSave) {
|
|
|
|
user.save().then(function () {
|
2019-06-08 18:51:24 +00:00
|
|
|
logger.debug(`user login: ${user.id}`)
|
2017-04-11 21:41:14 +00:00
|
|
|
return done(null, user)
|
|
|
|
})
|
|
|
|
} else {
|
2019-06-08 18:51:24 +00:00
|
|
|
logger.debug(`user login: ${user.id}`)
|
2017-04-11 21:41:14 +00:00
|
|
|
return done(null, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('auth callback failed: ' + err)
|
|
|
|
return done(err, null)
|
|
|
|
})
|
|
|
|
}
|