mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-12-18 01:31:44 -05:00
cc5db72369
Signed-off-by: David Mehren <dmehren1@gmail.com>
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import { User } from '../../models'
|
|
import { logger } from '../../logger'
|
|
|
|
export function passportGeneralCallback (
|
|
accessToken: string,
|
|
refreshToken: string,
|
|
profile: any,
|
|
done: (err?: Error | null, user?: User) => void
|
|
): void {
|
|
const stringifiedProfile = JSON.stringify(profile)
|
|
User.findOrCreate({
|
|
where: {
|
|
profileid: profile.id.toString()
|
|
},
|
|
defaults: {
|
|
profile: stringifiedProfile,
|
|
accessToken: accessToken,
|
|
refreshToken: refreshToken
|
|
}
|
|
}).then(function ([user, _]) {
|
|
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, undefined)
|
|
})
|
|
}
|