From 6569b1e98fbd2a84551bb80eef23294727899fd2 Mon Sep 17 00:00:00 2001 From: Jake Watkins Date: Thu, 30 May 2024 14:18:03 -0400 Subject: [PATCH] More cleanup after testing * Fixed the auth0 API URL path * Changed the searchMultipleEmailCases call to a .then().catch() in order to properly resolve the returned promise --- rules/link-users-by-email-with-metadata.js | 68 +++++++++++----------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/rules/link-users-by-email-with-metadata.js b/rules/link-users-by-email-with-metadata.js index 0251b9a..8d150ea 100644 --- a/rules/link-users-by-email-with-metadata.js +++ b/rules/link-users-by-email-with-metadata.js @@ -34,10 +34,10 @@ function linkUsersByEmailWithMetadata(user, context, callback) { // which might be mixed case (or not). Our second search is for the lowercase equivalent but only if two searches // would be different. const searchMultipleEmailCases = async () => { - const emailUrl = new URL('/users-by-email', auth0.baseUrl); + const emailUrl = new URL('/api/v2/users-by-email', auth0.baseUrl); emailUrl.searchParams.append('email', user.email); - const emailUrlToLower = new URL('/users-by-email', auth0.baseUrl); + const emailUrlToLower = new URL('/api/v2/users-by-email', auth0.baseUrl); emailUrlToLower.searchParams.append('email', user.email.toLowerCase()); let fetchPromiseArray = [fetch(emailUrl.toString(), opts)]; @@ -127,37 +127,37 @@ function linkUsersByEmailWithMetadata(user, context, callback) { }; // Search for multiple accounts of the same user to link - let data = searchMultipleEmailCases(); - - try { - // Ignore non-verified users - data = data.filter((u) => u.email_verified); - - if (data.length <= 1) { - // The user logged in with an identity which is the only one Auth0 knows about - // or no data returned - // Do not perform any account linking - return callback(null, user, context); - } + searchMultipleEmailCases() + .then((data) => { + // Ignore non-verified users + data = data.filter((u) => u.email_verified); + + if (data.length <= 1) { + // The user logged in with an identity which is the only one Auth0 knows about + // or no data returned + // Do not perform any account linking + return callback(null, user, context); + } - if (data.length === 2) { - // Auth0 is aware of 2 identities with the same email address which means - // that the user just logged in with a new identity that hasn't been linked - // into the other existing identity. Here we pass the other account to the - // linking function - linkAccount(data.filter((u) => u.user_id !== user.user_id)[0]); - } else { - // data.length is > 2 which, post November 2020 when all identities were - // force linked manually, shouldn't be possible - var error_message = - `Error linking account ${user.user_id} as there are ` + - `over 2 identities with the email address ${user.email} ` + - data.map((x) => x.user_id).join(); - console.log(error_message); - return callback(new Error(error_message)); - } - } catch (err) { - console.log('An unknown error occurred while linking accounts: ' + err); - return callback(err); - } + if (data.length === 2) { + // Auth0 is aware of 2 identities with the same email address which means + // that the user just logged in with a new identity that hasn't been linked + // into the other existing identity. Here we pass the other account to the + // linking function + linkAccount(data.filter((u) => u.user_id !== user.user_id)[0]); + } else { + // data.length is > 2 which, post November 2020 when all identities were + // force linked manually, shouldn't be possible + var error_message = + `Error linking account ${user.user_id} as there are ` + + `over 2 identities with the email address ${user.email} ` + + data.map((x) => x.user_id).join(); + console.log(error_message); + return callback(new Error(error_message)); + } + }) + .catch((err) => { + console.log('An unknown error occurred while linking accounts: ' + err); + return callback(err); + }); }