Auth0 Home Blog Docs

How do I get app_metadata via rules? Docs seem incomplete or confusing

rules
app_metadata
documentation

#1

Hello, I need to get the authorized user’s app_metadata and I’m having quite a bit of difficulty doing so. I have read the following Auth0 doc pages:

I still just don’t get it, but I think I’m close to understanding. It’s my understanding that I need to create a new rule to add scoped information to the context from the user profile - in my case I would be adding app_metadata. But when I try to test this rule on the “Edit Rule” page I get the error below:

function (user, context, callback) {
const namespace = ‘https://myapp.example.com/’;
//context.idToken[namespace + ‘favorite_color’] = user.favorite_color;
context.idToken[namespace + ‘preferred_contact’] = user.user_metadata.preferred_contact;
callback(null, user, context);
}

“ERROR: Cannot read property ‘preferred_contact’ of undefined”

The User object does not have the “user_metadata” object (nor does it have the “app_metadata” object). So right there the sample code in the “User profile claims and scope” doc page is not helpful and effectively wrong. It almost makes me think there is something wrong with my account? I’m using the “free” Auth0 account for now. It’s a “Generic Account” that I created from the Heroku app. Is it only the paid accounts that have metadata info in the User object? If that’s the case then that page of the docs should specify. Or maybe I need to login somehow to set my id_token in the session? But, again, the docs should specify that. there seems to be a lot of silent presumption in the docs on this particular subject and it’s forcing me to visit all the pages and tie it all together. Which, if my use case were somehow rare then it would be understandable. But these doc titles are specifically my use case and they still don’t seem to be able to just tell me how to accomplish my goals.

So thoughts? What am I doing wrong? I can post code, but I can’t even get the Rule Editor to work so I haven’t written much code in this regard yet, other than a simply *.userinfo() call which, of course comes back without any metadata info.

Thanks for any help. Auth0 is a fantastic product.


#3

THIS page also seems to suggest a rule that does not work for me.

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

Again, the ‘user_metadata’ object is not part of the user object by default for me. Am I doing something wrong?


#4

Hey there @sfncook, how long ago did you create your tenant by chance? As stated in the Metadata doc you referenced, the doc goes talks about the limitations of searching app_metadata:

Beginning 1 September 2017 , new tenants cannot search any of the app_metadata fields.

Only tenants associated with paid subscriptions that were created on/before 31 August 2017 can search the app_metadata fields.

We do have this doc here that dives into how you can create and update user’s metadata leveraging our API.

From looking at this thread below they had a similar pursuit:

Please let me know if I am going in the wrong direction or not fully understanding the desired outcome because I want to make sure I understand. Thanks in advance!


#5

Thanks, @James.Morrison! Hmm, i’m not sure about that, but I figured out a solution using the ManagementClient and API. Maybe the docs are written in a way that’s just too subtle for me? :slight_smile:

For posterity, here is the code that worked for me: https://gist.github.com/sfncook/a6000614b1649c0df52cba4b21003a0c


#6

@sfncook I’m glad it all came together and thanks for share the code sample!