mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 11:30:13 +08:00
feat: make social login optional
This commit is contained in:
1 parent
a79b66ed8d
commit
72b2711367
5 files changed
+145
-49
No files matched your search
@@ -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
|
||||
@@ -5,7 +5,17 @@ import googleStrategy from './googleStrategy.js'
|
||||
|
||||
passport.use(passwordStrategy)
|
||||
passport.use(jwtStrategy)
|
||||
passport.use(googleStrategy)
|
||||
|
||||
const enabledMethods = {
|
||||
local: true,
|
||||
google: googleStrategy !== null,
|
||||
}
|
||||
|
||||
console.log('Enabled login methods', enabledMethods)
|
||||
|
||||
if (enabledMethods.google) {
|
||||
passport.use(googleStrategy)
|
||||
}
|
||||
|
||||
passport.serializeUser((user, done) => {
|
||||
process.nextTick(() => {
|
||||
@@ -22,3 +32,5 @@ passport.deserializeUser((user, done) => {
|
||||
done(null, user)
|
||||
})
|
||||
})
|
||||
|
||||
export default enabledMethods
|
||||
Reference in new issue
Block a user