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 - #8 by James.Morrison

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);
});

});
}

Hi! How are you? were you able to solve this? I’m having exactly the same issue but using the new Actions.

I am seeing a similar issue too. Did you figure out a fix or workaround? Thanks

Hi

Yes, I actually was able to solve this issue after receiving help from their support engineers. I don’t remember a lot of this as I don’t use Auth0 anymore, but I managed to retreive the logs from the support conversation.

This is the response they sent me that solved the issue:

Looking at your rules, it looks like you have another rule (Add first login to the user profile) that is called after the Link Users with Same Email Address rule that is also using “user_id”, can you please try and update the rule to check if the user_id that is being used is correct by updating it to the following:

function (user, context, callback) {
var realUserId = context.primaryUser || user.user_id;
user.user_metadata = user.user_metadata || {};
user.user_metadata.first_login = typeof user.user_metadata.first_login === 'undefined'; const namespace = 'https://reservamos.mx/';
context.idToken[namespace + 'first_login'] = user.user_metadata.first_login; auth0.users.updateUserMetadata(realUserId, user.user_metadata)
.then(function(){
callback(null, user, context);
})
.catch(function(err){
callback(err);
});
}

Later I had a problem with the metadata of the user. After merging even if the user had already logged multiple times, its metadata showed that it was a first login.

I was able to solve this after long exchange of messages with support. If you need further help I can copy the whole conversation and post it here, but maybe the problem has already been fixed by them.

Hope this helps!

2 Likes

Thanks for sharing that with the rest of community!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.