Update user.email in a rule

I would like all my users to have an email address. I’ve added this rule:

function (user, context, callback) {
  if (!user.email) {
    if (context.connection === 'twitter') {
      user.email = user.screen_name + '@users.twitter.com';
    } else if (context.connection === 'facebook') {
      var fbIdentity = _.find(user.identities, { connection: 'facebook' });
      user.email = fbIdentity.user_id + '@users.facebook.com';
    } else {
      user.email = user.user_id + '@users.auth0.com';
    }
    console.log("New email: " + user.email);
  }

  callback(null, user, context);
}

It does not seam to work. I do see that the log output is correct but the email is not saved on the user. Am I doing something wrong? Am I allowed to edit the email?

(I used the “Get email address from Twitter”-rule as template)

Based on the information provided I would advise against even attempting to make that work as what you trying to set does not map to a real email address inbox; it just tries to mimic the email address format. There are only a few edge cases where setting the user email property to something that is not mapped to a real inbox would be required and this one does not seem to be the case.

If you want an email like identifier in addition to the user identifier already assigned by Auth0 then you should likely store that as part of app_metadata in a property specific to that effect, for example app_metadata.internal_id. In addition, the use of Twitter screen name is also suspicious as that may be mutable and cause either issues or additional complexity in managing it. See the following link for how to update metadata from a rule: Manage Metadata with Rules

Thank you for the feedback and suggestions.
There is some legacy apps that uses the email as an identifier. We are moving away for that ATM but I need a solution that works with the existing code base.

Apart for it being a stupid idea, is it possible to update user.email in a rule?

Thank you for the feedback and suggestions.
There is some legacy apps that uses the email as an identifier. We are moving away for that ATM but I need a solution that works with the existing code base.

Apart for it being a stupid idea, is it possible to update user.email in a rule?

The root email field at the profile may come from an external social provider so in those case there’s no way for you to directly update it. For database connections users you could technically call Management API from a rule to update an email, but I would also not recommend it.