Retrieving app_metadata section from the user once logged in

Hi,
I successfully deployed MAUI application to my devices and authenticating with Auth0 works on both IOS/Android platforms. I was wondering what is the best way to retrieve additional sections fromt the user, such as app_metadata where I am adding the roles for the user.
I am a C# developer and any help on this topicwould be much appreciated, thanks!

Ross.

Hey there @rosario.martone.uk welcome to the community!

The most effective way is probably to add the metadata to tokens and go about it that way - You need to add the metadata as a custom claim(s) using an action:

An example might look like:

exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://myapp.example.com';
  const { favorite_color, preferred_contact } = event.user.user_metadata;

  if (event.authorization) {
    // Set claims 
    api.idToken.setCustomClaim(`${namespace}/favorite_color`, favorite_color);
    api.idToken.setCustomClaim(`${namespace}/preferred_contact`, preferred_contact);
  }
};
1 Like

Hi,
thanks for this example. This is exactly what I did but it still not working.

Response from userinfo:
{
“nickname”: “rosario.martone”,
“name”: “rosario.martone@gmail.com”,
“picture”: “https://s.gravatar.com/avatar/7c988bb6e283c264a5a85fa0ba979820?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fro.png”,
“updated_at”: “2024-03-06T06:43:19.5Z”,
“iss”: “https://dev-70vjf83lo03hdc1r.uk.auth0.com/”,
“aud”: “7zJzL2KUozIiltrFXgpSVWjbGp0lU5mh”,
“iat”: 1709707400,
“exp”: 1709743400,
“sub”: “auth0|65e42f567c644203f3b666d7”,
“sid”: “p7eXDj8p7YQnQHBFQ2m4HEa9lBUM2z9r”
}

This is the user_metadata section on the Auth0 user:
{
“roles”: “admin”
}

This is the custom function I have added to the login process:
/**

  • Handler that will be called during the execution of a PostLogin flow.

  • @param {Event} event - Details about the user and the context in which they are logging in.

  • @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
    */
    exports.onExecutePostLogin = async (event, api) => {
    const namespace = ‘https://dev-70vjf83lo03hdc1r.uk.auth0.com’;
    const { roles } = event.user.user_metadata;

    // Adds your data to the id token
    if (event.authorization) {
    // Set claims
    api.idToken.setCustomClaim(${namespace}/roles, roles);
    }
    };

I have no idea what else I can try orwhere I am mistaken, any help would be much appreciated!

Thanks,
Ross.

1 Like

Also, this is the scope I have tried, not sure it’s correct:

Scope = “openid profile read:current_user”

1 Like

I also have tried this: Auth0: how to include app_metadata in user info
I ran out of ideas! :slight_smile:

Ross

1 Like

Thanks for following up!

It might be due to the fact that you’re using an auth0 domain as your namespace which isn’t allowed:

1 Like

Hi,
That was exactly the issue and you saved my day.

Cheers,
Ross

1 Like

Awesome, glad to hear that resolved it - Thanks for confirming!

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