refactor: reuse oauthVerify function

This commit is contained in:
wancat committed 2023-10-13 00:51:00 +08:00
1 parent 9253feb520
commit 7a9206fb47
3 files changed
+28 -66

No files matched your search

+2 -33
View File
@@ -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