User does not exist after account linking

Hi!

I’m having the same issue as in: Link users by email rule - THE USER DOES NOT EXIST

I didn’t see any solution on the thread, and the workaround proposed didn’t work.

So the problem is that the first login right after the account linking, THE USER DOES NOT EXIST is returned.

The second login works as expected.

This is the error log of the failed login:
“error”: {
“message”: “The user does not exist.”,
“oauthError”: “access_denied”,
“type”: “oauth-authorization”
}

Has anyone experienced this before? If so, how was it fixed?

Thanks!

1 Like

Hi @marcel2, and welcome to Auth0 Community :tada:

Can you please share your rule?

One thing worth doing here is to debug the Rules using console.log statements, and monitoring the output using Realtime Webtask Logs.

Hope to hear back from you soon.

Hi Lily

We can’t see anything weird going on. Here is the rule we are using. It is the same one as the default one.

function (user, context, callback) {
  const request = require('request');
  // Check if email is verified, we shouldn't automatically
  // merge accounts if this is not the case.
  if (!user.email || !user.email_verified) {
    return callback(null, user, context);
  }
  const userApiUrl = auth0.baseUrl + '/users';
  const userSearchApiUrl = auth0.baseUrl + '/users-by-email';

  request({
    url: userSearchApiUrl,
    headers: {
      Authorization: 'Bearer ' + auth0.accessToken
    },
    qs: {
      email: user.email
    }
  },
  function(err, response, body) {
    if (err) return callback(err);
    if (response.statusCode !== 200) return callback(new Error(body));

    var data = JSON.parse(body);
    // Ignore non-verified users and current user, if present
data = data.filter(function(u) {
  return u.email_verified && (u.user_id !== user.user_id);
});

if (data.length > 1) {
  return callback(new Error('[!] Rule: Multiple user profiles already exist - cannot select base profile to link with'));
}
if (data.length === 0) {
  console.log('[-] Skipping link rule');
  return callback(null, user, context);
}

const originalUser = data[0];
const provider = user.identities[0].provider;
const providerUserId = user.identities[0].user_id;

request.post({
  url: userApiUrl + '/' + originalUser.user_id + '/identities',
  headers: {
    Authorization: 'Bearer ' + auth0.accessToken
  },
  json: {
    provider: provider,
    user_id: String(providerUserId)
  }
}, function(err, response, body) {
  if (response.statusCode >= 400) {
    return callback(new Error('Error linking account: ' + response.statusMessage));
  }
  context.primaryUser = originalUser.user_id;
  callback(null, originalUser, context);
});

});
}