Actions: How do I *set* an event.user.nickname?

I have this action:

exports.onExecutePostLogin = async (event, api) => {
  // Put the user screen_name as the nickname
  // for Twitter connections
  if (event.connection.name === 'twitter' && event.user.screen_name) {
    event.user.nickname = event.user.screen_name;
  } 
};

This doesn’t set the user’s nickname.

I can do this: api.user.setUserMetadata("nickname", "event.user.screen_name");
But that only adds it to the metadata.

I need the user’s object to contain the data.

Is there any way to set the nickname using an action?

Incidentally, this is a workaround for a bug that’s now 5 years old…

hey @edent :wave:

I can’t speak much regarding the bug you mention from 5 years ago, but if you need to update something other than user_metadata or app_metadata on user profiles in Auth0 Actions, you will have to use the Management API.

See this FAQ for more info on how to set it up

Also, as noted in the FAQ above, the Management API updates are rate limited, so be sure to also add a check that the user’s event.user.nickname doesn’t already match event.user.screen_name before making the Management API request.

Hope this helps :crossed_fingers:

EDIT:
Something I forgot to mention, if you need to update nickname on social users, please note the following from this doc Configure Identity Provider Connection for User Profile Updates

By default, user profile attributes provided by identity providers other than Auth0 (such as Google, Facebook, Twitter) are not directly editable because they are updated from the identity provider each time the user logs in.

To be able to edit the name, nickname, given_name, family_name, or picture root attributes on the normalized user profile, you must configure your connection sync with Auth0 so that user attributes will be updated from the identity provider only on user profile creation.

1 Like

Wait, I have to set up and configure another application, to call an API, to fix a bug, all because Auth0 have decided to deprecate something that worked?

Can I just pay someone at Okta to fix this bug?

Seriously, gang, I’ve got £5 here for any employee of Auth0 who wants to make this right.

Hey @edent

I took a closer look at the older post you made, are you looking to just temporarily override the nickname field for the ID Token on the frontend or the Access Token for the /userinfo endpoint?

I haven’t tested this, but try setting the nickname via the built-in api setCustomClaim functions.

exports.onExecutePostLogin = async (event, api) => {
  // Put the user screen_name as the nickname
  // for Twitter connections
  if (event.connection.name === 'twitter' && event.user.screen_name) {
    api.idToken.setCustomClaim('nickname', event.user.screen_name);
    api.accessToken.setCustomClaim('nickname', event.user.screen_name);
  } 
};

Generally you would create a custom claim in the tokens with a namespace you own, but give this a try and see if this will resolve your issue.

1 Like

That seems to work! Thank you.

Nevertheless, I think it’s time for me to move away from Auth0 if I can. It’s quite clear that they’ve no interest in fixing bugs and would rather spend their time changing their logo.

1 Like

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