Auth0 Home Blog Docs

How to get user_metadata and app_metadata in id_token

jwt
#1

I am trying to get the user_metadata and app_metadata with the id_token when logging in. I have made a rule to pass them with the id_token but when I log in from my react app the user_metadata and app_metadata aren’t available.
here is my rule

function (user, context, callback) {
  user.app_metadata = user.app_metadata || {};
  user.user_metadata = user.user_metadata || {};
  var namespace = 'https://example.auth0.com/';
  if (user.user_metadata) {
    context.idToken[namespace + 'user_authorization'] = {
      groups: user.user_metadata.groups,
      status: user.app_metadata.groups,
    };
  }
  return callback(null, user, context);
}
#3

Hi there @abellamesgen2, we actually have a few sample rules that feed off the user_metadata like the one below. This could be a great reference point when building out new rules. I have also included our rules best practices document at the bottom for historical context. Please let me know if these docs are useful to you or if you could see where we can better adjust them to fit your needs. Thanks in advance!

function(user, context, callback) {

  // copy user metadata value in ID Token
  context.idToken['http://fiz/favorite_color'] = user.user_metadata.favorite_color;

  callback(null, user, context);
}
#4

I wanted to follow up @abellamesgen21 and see if you had any additional questions on this front. Thanks!

closed #5

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