mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +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 GitHubStrategy from 'passport-github2'
|
||||
|
||||
import config from '../config.js'
|
||||
import User from '../models/user.js'
|
||||
import oauthVerify from './oauthVerify.js'
|
||||
|
||||
function verify(accessToken, refreshToken, 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({ 'github.id': profile.id })
|
||||
if (!user) {
|
||||
user = await User.findOne({ email })
|
||||
const github = {
|
||||
id: profile.id,
|
||||
displayName: profile.displayName,
|
||||
}
|
||||
if (!user) {
|
||||
const newUser = {
|
||||
email,
|
||||
github,
|
||||
}
|
||||
cb(null, { newUser })
|
||||
return
|
||||
}
|
||||
if (user.github?.id) {
|
||||
cb(new Error('Email already used and binded to another account'))
|
||||
return
|
||||
}
|
||||
user.github = github
|
||||
await user.save()
|
||||
}
|
||||
|
||||
cb(null, user)
|
||||
})
|
||||
oauthVerify('github', profile, { 'github.id': profile.id }, cb)
|
||||
}
|
||||
|
||||
const enabled = config.github.clientID && config.github.clientSecret
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import User from '../models/user.js'
|
||||
|
||||
export default async function oauthVerify(provider, profile, query, cb) {
|
||||
process.nextTick(async () => {
|
||||
const email = profile.emails[0]?.value
|
||||
if (!email) {
|
||||
return cb(new Error('No email found'))
|
||||
}
|
||||
|
||||
let user = await User.findOne(query)
|
||||
if (!user) {
|
||||
user = await User.findOne({ email })
|
||||
if (!user) {
|
||||
const newUser = { email }
|
||||
newUser[provider] = profile
|
||||
return cb(null, { newUser })
|
||||
}
|
||||
user[provider] = profile
|
||||
await user.save()
|
||||
}
|
||||
|
||||
return cb(null, user)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user