2020-04-13 05:23:24 -04:00
|
|
|
import { User } from '../../models'
|
|
|
|
import { logger } from '../../logger'
|
2017-04-11 17:41:14 -04:00
|
|
|
|
2020-04-13 06:12:44 -04:00
|
|
|
export function passportGeneralCallback (
|
2020-04-13 08:56:53 -04:00
|
|
|
accessToken: string,
|
|
|
|
refreshToken: string,
|
|
|
|
profile: any,
|
|
|
|
done: (err?: Error | null, user?: User) => void
|
2020-04-13 05:23:24 -04:00
|
|
|
): void {
|
|
|
|
const stringifiedProfile = JSON.stringify(profile)
|
|
|
|
User.findOrCreate({
|
2017-04-11 17:41:14 -04:00
|
|
|
where: {
|
|
|
|
profileid: profile.id.toString()
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
profile: stringifiedProfile,
|
|
|
|
accessToken: accessToken,
|
|
|
|
refreshToken: refreshToken
|
|
|
|
}
|
2020-04-13 05:23:24 -04:00
|
|
|
}).then(function ([user, _]) {
|
2017-04-11 17:41:14 -04:00
|
|
|
if (user) {
|
2020-04-13 05:23:24 -04: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)
|
2020-04-13 08:56:53 -04:00
|
|
|
return done(err, undefined)
|
2017-04-11 17:41:14 -04:00
|
|
|
})
|
|
|
|
}
|