mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
refactor: reuse oauthVerify function
This commit is contained in:
1 parent
9253feb520
commit
7a9206fb47
3 files changed
+28
-66
No files matched your search
@@ -1,41 +1,10 @@
|
||||
import GoogleStrategy from 'passport-google-oidc'
|
||||
|
||||
import config from '../config.js'
|
||||
import User from '../models/user.js'
|
||||
import oauthVerify from './oauthVerify.js'
|
||||
|
||||
function verify(issuer, profile, cb) {
|
||||
process.nextTick(async () => {
|
||||
const email = profile.emails[0]?.value
|
||||
if (!email) {
|
||||
cb(new Error('No email found'))
|
||||
return
|
||||
}
|
||||
|
||||
let user = await User.findOne({ 'google.id': profile.id })
|
||||
if (!user) {
|
||||
user = await User.findOne({ email })
|
||||
const google = {
|
||||
id: profile.id,
|
||||
displayName: profile.displayName,
|
||||
}
|
||||
if (!user) {
|
||||
const newUser = new User({
|
||||
email,
|
||||
google,
|
||||
})
|
||||
cb(null, { newUser })
|
||||
return
|
||||
}
|
||||
if (user.google?.id) {
|
||||
cb(new Error('Email already used and binded to another account'))
|
||||
return
|
||||
}
|
||||
user.google = google
|
||||
await user.save()
|
||||
}
|
||||
|
||||
cb(null, user)
|
||||
})
|
||||
oauthVerify('google', profile, { 'google.id': profile.id }, cb)
|
||||
}
|
||||
|
||||
const enabled = config.google.clientID && config.google.clientSecret
|
||||
|
||||
Reference in new issue
Block a user