2

I'm a beginner with auth0 and Angular 2. I'm trying to use Auth0 to authentication and authorization on my app. Below the code I use to initialize the authentication

@Injectable()
export class Auth {
  //Configure Auth0
  lock = new Auth0Lock('CLIENT_ID','<myapp>.eu.auth0.com', {
    auth: {
      params: {
        scope: 'openid profile user_metadata app_metadata',
        audience: 'https://<myapp>.eu.auth0.com/api/v2/'
      }
    }
  });

  userProfile;

  constructor(private router: Router) {

    this.userProfile = JSON.parse(localStorage.getItem("profile"));

    // Add callback for lock `authenticated` event
    this.lock.on("authenticated", (authResult) => {
      localStorage.setItem('id_token', authResult.accessToken);

      console.log(authResult);

      this.lock.getUserInfo(authResult.accessToken, (error, profile) => {
        if (error) {
          console.log("Error:", error);
          return;
        }

        localStorage.setItem("profile", JSON.stringify(profile));

        this.userProfile = profile;


      });
    });
  }

when I log the profile I can't see the app_metadata in the profile json and also I can't see it in the JWT Token.

Array[5] 0:"sub" 1:"name" 2:"nickname" 3:"picture" 4:"updated_at"

I need it to get the roles from the user profile.

In the first place I've used the getProfile method that returned me the correct metadata but I read that it will be deprecated so I replaced it with getUserInfo but it works in a different way.

I noticed that if I delete the audience parameter the authResult contains the app_metadata informations like roles, but I receive "JWT must have 3 parts" error from the console.

There is something I miss understand about how it work.

Please, can anybody help me? Thanks.

  • Please read https://stackoverflow.com/questions/42374427/include-user-metadata-and-app-metadata-in-jwt-using-auth0/46540177#46540177 I had the same problem as you. – Jesús Flores Oct 03 '17 at 08:25

0 Answers0