Angular SDK Subscribe crash

Please include the following information in your post:

  • Which SDK this is regarding: Angular SDK
  • SDK Version: @auth0/auth0-angular": “^1.3.2”
  • Platform Version: Node v14.16.0

When i try to subscribe and get the user profile, its making a bunch of the authentication calls. I just want to make one and based on the app_metadata returned, redirect my user to the appropriate route. Below is the code that I have. Any suggestions:

   this.authService.user$.subscribe((data) => {
     if(data){
       this.http.get(`https://${domain}/api/v2/users/${data.sub}`).subscribe(
         user => {
          this.profile = user
          if(this.profile.app_metadata){
            this.companyId = this.profile.app_metadata.companyId
          }
         }
         )
     }
  });


    if(isNumber(this.companyId)){
      this.router.navigate(["my-client", this.companyId])
    }else {
      this.router.navigate(["client"])
    }

Hi @oflores2313,

Since you require user metadata to determine the redirect, you may want to add a custom claim to the ID Token instead of making a call to the Management API.

To do this, you can use a post-login action:

exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://test.com/'; // <-- custom claims require a URI namespace
  api.idToken.setCustomClaim(`${namespace}companyId`, event.user.app_metadata.companyId);
};

In your app, you can find the companyID without making an API request:

this.auth.user$.subscribe(
      (profile) => (this.companyId = profile['https://test.com/companyId'])
    );
    // ...

or in a template:

<p>{{ user['https://test.com/companyId'] }}</p>

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