function(user, context, callback){
user.user_metadata = user.user_metadata || {};
// update the user_metadata that will be part of the response
user.user_metadata.preferences = user.user_metadata.preferences || {};
user.user_metadata.preferences.fontSize = 12;
// persist the user_metadata update
auth0.users.updateUserMetadata(user.user_id, user.user_metadata)
.then(function(){
callback(null, user, context);
})
.catch(function(err){
callback(err);
});
}
After many attempts to add metadata to my user object, I decided to paste the example above into my rules (which is directly from the API docs), and it does not seem to work. The fontSize property does not appear.
Thus far I have only seen properties added to my user object with this method –
However I do not want the property to be namespaced in that way. I would like it to be named color. The properties I want to add are not concerned with idToken.
The first example works when I look at the response in the webtask extension (I can see the user_metadata field), but it does not manifest in the frontend of my app, only the default fields.
First, I have tested the Rule snippet and was able to get it to work. I can confirm the doc example works correctly without issues.
In this case, I recommend you check the user’s profile details to verify if the user.user_metadata.preferences has already been created. If so, you will want to delete the property and retry again.
As an alternative, there is also the option to use the Auth0 Post-Login Action to accomplish the same results. For example:
I was able to hack this a bit, got this result by adding subfields to a namespaced field. Doesn’t fully solve the issue but can live with it. I’m going to change the name on the frontend and add to my global store and pull from there.
Having looked closely at your screenshots, it appears that your Rule is updating the user’s user_metadata and appending them as custom claims correctly. Everything seems fine.
Could you please clarify the issues you are experiencing now?
Sure, I dont want to use the namespaced claim. When I access the user_metadata from the Auth0 hook on the frontend I currently need to access it like this:
I’m trying to understand why the first example in this thread cannot do that, and why I must use this namespaced field (context.idToken[...). It looks messy, sucks to type, and I will probably have to explain it to my teammates in the future because it is a bit weird.
Unfortunately, this is not possible with the useAuth0 hook’sgetIdTokenClaims method because it is used to get the claims from the ID Token. In this scenario, the recommendation of appending them to a namespaced claim is the only way to get the user’s user_metadata in the ID token.
As an alternative, you could consider using the Management API get a user endpoint in conjunction with the Post-Login action mentioned here. This way, you can get the user_metadata calling user.user_metadata.user_guid instead of calling the user_metadata as a custom namespaced claim.
Hoped this helps!
Please let me know if you need further clarification or have any questions.