From 5aa924f02fe6aba3a22b19cd95ee4f64f2eafefe Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Thu, 11 Nov 2021 12:57:39 +0100 Subject: [PATCH] Allow password reset emails to be sent to social login users --- website/server/controllers/api-v3/auth.js | 9 ++++++++- website/server/libs/auth/social.js | 15 +++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/website/server/controllers/api-v3/auth.js b/website/server/controllers/api-v3/auth.js index 7860a3db61..6b7c16636e 100644 --- a/website/server/controllers/api-v3/auth.js +++ b/website/server/controllers/api-v3/auth.js @@ -341,7 +341,14 @@ api.resetPassword = { if (validationErrors) throw validationErrors; const email = req.body.email.toLowerCase(); - const user = await User.findOne({ 'auth.local.email': email }).exec(); + const user = await User.findOne({ + $or: [ + { 'auth.local.email': email }, + { 'auth.apple.emails.value': email }, + { 'auth.google.emails.value': email }, + { 'auth.facebook.emails.value': email }, + ], + }).exec(); if (user) { // create an encrypted link to be used to reset the password diff --git a/website/server/libs/auth/social.js b/website/server/libs/auth/social.js index da01d790e3..33d8282a7e 100644 --- a/website/server/libs/auth/social.js +++ b/website/server/libs/auth/social.js @@ -24,7 +24,7 @@ function _passportProfile (network, accessToken) { } export async function loginSocial (req, res) { // eslint-disable-line import/prefer-default-export - const existingUser = res.locals.user; + let existingUser = res.locals.user; const { network } = req.body; const isSupportedNetwork = common.constants.SUPPORTED_SOCIAL_NETWORKS @@ -48,16 +48,15 @@ export async function loginSocial (req, res) { // eslint-disable-line import/pre // User already signed up if (user) { if (existingUser) { - throw new NotAuthorized(res.t('socialAlreadyExists')) + throw new NotAuthorized(res.t('socialAlreadyExists')); } return loginRes(user, req, res); } - - const email; + let email; if (profile.emails && profile.emails[0] && profile.emails[0].value) { - email = profile.emails[0].value.toLowerCase() - } + email = profile.emails[0].value.toLowerCase(); + } if (!existingUser) { existingUser = await User.findOne({ 'auth.local.email': email }, { 'auth.local': 1 }).exec(); @@ -78,7 +77,7 @@ export async function loginSocial (req, res) { // eslint-disable-line import/pre local: { username: generatedUsername, lowerCaseUsername: generatedUsername, - email: email; + email, }, }, profile: { @@ -106,7 +105,7 @@ export async function loginSocial (req, res) { // eslint-disable-line import/pre // Clean previous email preferences if (email) { EmailUnsubscription - .remove({ email: email }) + .remove({ email }) .exec() .then(() => { if (!existingUser) {