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 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
|
||||
|
||||
Reference in new issue
Block a user