User.app_metadata not available when subscribing to user$

I have an Angular11 application that I have integrated Auth0-angular library into. I have also created a rule that on login checks to see if user.app_metadata.stripe_customer exists, if it doesn’t exist it then goes about creating a stripe customer with stripes API, a stripe subscription and attaching it to that customer, and then updating user.app_metadata with the stripe customer information. If the stripe_customer exists it will grab the latest information from stripe and update user.app_metadata.stripe_customer with the new data. This functionality works.

I now want to make use of this user.app_metadata in my application. For example, show a user when their subscription ends, or if it has ended redirect them to a page to purchase a new paid subscription. However the code to get the user

    this.auth.user$.subscribe(user => {
      console.log(user);
      console.log(user.app_metadata);
    });

When I register or login. user.app_metadata does not exist, and looking through the documentation I don’t see any way within the Auth0-angular library to get this data. Could someone point me in the right direction?

Hi @thenetimp,

The user object that the Angular SDK exposes is the decoded ID token. Here’s an example of profile info that would be included in an ID Token: ID Token Structure

You can extend the data that is included in the ID Token by adding this to your rule:

function(user, context, callback) {
  // ... handle stripe customer logic
  const namespace = 'https://myapp.example.com/';
  context.idToken[namespace + 'stripe_customer'] = user.app_metadata.stripe_customer; // You could also include all of app_metadata instead
  callback(null, user, context);
}

In your Angular app, you can retrieve the custom claim like this:

    this.auth.user$.subscribe(user => {
      console.log(user['https://myapp.example.com/stripe_customer']);
    });

Here is more information about adding custom claims:

1 Like

I was just coming here to reply to my own ticket, cause I had just figured that out on my own. Thanks for replying in a quick manner though.

2 Likes

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