From 8430ce442857b1f648bdcdde44ace8bfeb0fe7ad Mon Sep 17 00:00:00 2001 From: wancat Date: Sat, 18 Nov 2023 21:46:03 +0800 Subject: [PATCH] feat: add error handling for oauthVerify --- server/src/auth/oauthVerify.js | 44 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/server/src/auth/oauthVerify.js b/server/src/auth/oauthVerify.js index 8699ca8..ea4aedd 100644 --- a/server/src/auth/oauthVerify.js +++ b/server/src/auth/oauthVerify.js @@ -2,27 +2,31 @@ import { User } from '@linux-odyssey/models' 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')) - } - - const socialLogin = { - provider, - id: profile.id, - displayName: profile.displayName, - } - let user = await User.findOne(query) - if (!user) { - user = await User.findOne({ email }) - if (!user) { - const newUser = { email, socialLogin } - return cb(null, { newUser }) + try { + const email = profile.emails[0]?.value + if (!email) { + return cb(new Error('No email found')) } - user.socialLogins.set(provider, socialLogin) - await user.save() - } - return cb(null, user) + const socialLogin = { + provider, + id: profile.id, + displayName: profile.displayName, + } + let user = await User.findOne(query) + if (!user) { + user = await User.findOne({ email }) + if (!user) { + const newUser = { email, socialLogin } + return cb(null, { newUser }) + } + user.socialLogins.set(provider, socialLogin) + await user.save() + } + + return cb(null, user) + } catch (err) { + return cb(err) + } }) }