2017-04-11 17:41:14 -04:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const models = require('../../models')
|
|
|
|
const logger = require('../../logger')
|
|
|
|
|
|
|
|
exports.passportGeneralCallback = function callback (accessToken, refreshToken, profile, done) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const stringifiedProfile = JSON.stringify(profile)
|
2017-04-11 17:41:14 -04:00
|
|
|
models.User.findOrCreate({
|
|
|
|
where: {
|
|
|
|
profileid: profile.id.toString()
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
profile: stringifiedProfile,
|
2022-05-01 15:14:27 -04:00
|
|
|
accessToken,
|
|
|
|
refreshToken
|
2017-04-11 17:41:14 -04:00
|
|
|
}
|
|
|
|
}).spread(function (user, created) {
|
|
|
|
if (user) {
|
2021-02-15 03:42:51 -05:00
|
|
|
let needSave = false
|
2017-04-11 17:41:14 -04:00
|
|
|
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 14:51:24 -04:00
|
|
|
logger.debug(`user login: ${user.id}`)
|
2017-04-11 17:41:14 -04:00
|
|
|
return done(null, user)
|
|
|
|
})
|
|
|
|
} else {
|
2019-06-08 14:51:24 -04:00
|
|
|
logger.debug(`user login: ${user.id}`)
|
2017-04-11 17:41:14 -04:00
|
|
|
return done(null, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('auth callback failed: ' + err)
|
|
|
|
return done(err, null)
|
|
|
|
})
|
|
|
|
}
|