feat: make social login optional

This commit is contained in:
Lin, Hung-Shin committed 2023-10-05 13:32:02 +08:00
1 parent a79b66ed8d
commit 72b2711367
5 files changed
+145 -49

No files matched your search

+43 -39
View File
@@ -3,48 +3,52 @@ import GoogleStrategy from 'passport-google-oidc'
import config from '../config.js'
import User from '../models/user.js'
console.log('Google config:', config.google)
function verify(issuer, profile, cb) {
console.log('Get a google login:', profile)
export default new GoogleStrategy(
{
clientID: config.google.clientID,
clientSecret: config.google.clientSecret,
callbackURL: '/api/v1/auth/google/callback',
scope: ['profile', 'email'],
},
function verify(issuer, profile, cb) {
console.log('Get a google login:', profile)
process.nextTick(async () => {
const email = profile.emails[0]?.value
if (!email) {
cb(new Error('No email found'))
return
}
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,
}
let user = await User.findOne({ 'google.id': profile.id })
console.log('Existing user:', user)
if (!user) {
user = await User.findOne({ email })
const google = {
id: profile.id,
displayName: profile.displayName,
}
console.log('Existing user:', user)
if (!user) {
user = new User({
email,
google,
})
} else if (user.google?.id) {
cb(new Error('Email already used and binded to another account'))
return
} else {
user.google = google
}
await user.save()
user = new User({
email,
google,
})
} else if (user.google?.id) {
cb(new Error('Email already used and binded to another account'))
return
} else {
user.google = google
}
await user.save()
}
cb(null, user)
})
}
)
cb(null, user)
})
}
const enabled = config.google.clientID && config.google.clientSecret
export default enabled
? new GoogleStrategy(
{
clientID: config.google.clientID,
clientSecret: config.google.clientSecret,
callbackURL: '/api/v1/auth/google/callback',
scope: ['profile', 'email'],
},
verify
)
: null