From 7a9206fb47f16af91a90cd61c6c61cf500b1b177 Mon Sep 17 00:00:00 2001 From: wancat Date: Fri, 13 Oct 2023 00:51:00 +0800 Subject: [PATCH] refactor: reuse oauthVerify function --- server/src/auth/githubStrategy.js | 35 ++----------------------------- server/src/auth/googleStrategy.js | 35 ++----------------------------- server/src/auth/oauthVerify.js | 24 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 66 deletions(-) create mode 100644 server/src/auth/oauthVerify.js diff --git a/server/src/auth/githubStrategy.js b/server/src/auth/githubStrategy.js index ae4e5d7..d3af989 100644 --- a/server/src/auth/githubStrategy.js +++ b/server/src/auth/githubStrategy.js @@ -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 diff --git a/server/src/auth/googleStrategy.js b/server/src/auth/googleStrategy.js index 749baa3..566a09e 100644 --- a/server/src/auth/googleStrategy.js +++ b/server/src/auth/googleStrategy.js @@ -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 diff --git a/server/src/auth/oauthVerify.js b/server/src/auth/oauthVerify.js new file mode 100644 index 0000000..55b95ee --- /dev/null +++ b/server/src/auth/oauthVerify.js @@ -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) + }) +}