Skip to content

Commit

Permalink
More cleanup after testing
Browse files Browse the repository at this point in the history
        * Fixed the auth0 API URL path
        * Changed the searchMultipleEmailCases call to a .then().catch()
          in order to properly resolve the returned promise
  • Loading branch information
dividehex committed May 30, 2024
1 parent 8435755 commit 6569b1e
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions rules/link-users-by-email-with-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand Down Expand Up @@ -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);
});
}

0 comments on commit 6569b1e

Please sign in to comment.