mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-28 17:50:57 -05:00
d26dcd04a1
Signed-off-by: David Mehren <git@herrmehren.de>
46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
'use strict'
|
|
|
|
const models = require('../../models')
|
|
const logger = require('../../logger')
|
|
|
|
exports.passportGeneralCallback = function callback (accessToken, refreshToken, profile, done) {
|
|
const stringifiedProfile = JSON.stringify(profile)
|
|
models.User.findOrCreate({
|
|
where: {
|
|
profileid: profile.id.toString()
|
|
},
|
|
defaults: {
|
|
profile: stringifiedProfile,
|
|
accessToken,
|
|
refreshToken
|
|
}
|
|
}).spread(function (user, created) {
|
|
if (user) {
|
|
let 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 () {
|
|
logger.debug(`user login: ${user.id}`)
|
|
return done(null, user)
|
|
})
|
|
} else {
|
|
logger.debug(`user login: ${user.id}`)
|
|
return done(null, user)
|
|
}
|
|
}
|
|
}).catch(function (err) {
|
|
logger.error('auth callback failed: ' + err)
|
|
return done(err, null)
|
|
})
|
|
}
|