BREAKING: change user social login schema

This commit is contained in:
wancat committed 2023-11-18 16:29:37 +08:00
1 parent 9e935b320a
commit 81581c4635
8 files changed
+59 -32

No files matched your search

+1 -1
View File
@@ -4,7 +4,7 @@ import config from '../config.js'
import oauthVerify from './oauthVerify.js'
function verify(accessToken, refreshToken, profile, cb) {
oauthVerify('github', profile, { 'github.id': profile.id }, cb)
oauthVerify('github', profile, { 'socialLogins.github.id': profile.id }, cb)
}
const enabled = config.github.clientID && config.github.clientSecret
+1 -1
View File
@@ -4,7 +4,7 @@ import config from '../config.js'
import oauthVerify from './oauthVerify.js'
function verify(issuer, profile, cb) {
oauthVerify('google', profile, { 'google.id': profile.id }, cb)
oauthVerify('google', profile, { 'socialLogins.google.id': profile.id }, cb)
}
const enabled = config.google.clientID && config.google.clientSecret
+7 -6
View File
@@ -7,18 +7,19 @@ export default async function oauthVerify(provider, profile, query, cb) {
return cb(new Error('No email found'))
}
const socialLogin = {
provider,
id: profile.id,
displayName: profile.displayName,
}
let user = await User.findOne(query)
if (!user) {
user = await User.findOne({ email })
if (!user) {
const newUser = { email }
newUser[provider] = profile
const newUser = { email, socialLogin }
return cb(null, { newUser })
}
user[provider] = {
id: profile.id,
displayName: profile.displayName,
}
user.socialLogins.set(provider, socialLogin)
await user.save()
}
+11 -7
View File
@@ -26,14 +26,18 @@ if (enabledMethods.github) {
passport.serializeUser((user, done) => {
process.nextTick(async () => {
if (!(await UserProfile.exists({ user: user._id }))) {
UserProfile.create({ user: user._id })
try {
if (user._id && !(await UserProfile.exists({ user: user._id }))) {
await UserProfile.create({ user: user._id })
}
done(null, {
_id: user._id,
username: user.username,
email: user.email,
})
} catch (err) {
done(err)
}
done(null, {
_id: user._id,
username: user.username,
email: user.email,
})
})
})