How save user info in a model

I would like to save the user’s info in a model in angular 5. The only way to know to fetch user’s info is in HTML calling the variable who was defined in the .ts like “{{profile?.given_name}}”. I need to use user’s info in the component.ts, not HTML. I tried to use the same variable in component.ts but for some reason that I don’t know doesn’t work if someone can send links of documentation or an example of the code, it would be great because I haven’t found the info using google.

Hey there, thanks for posting in the community! I’m not exactly sure what you’re asking – can you perhaps post some sample code? Are you using a service to store your information from Auth0?

2 Likes

Thank for replying!
I’m using the same code that is in the “quick start”. Now I don’t have a service to store the auth0 data.
the point is that I want to save auth0 info in my own database by a POST to a rest API.
I’m using this https://auth0.com/docs/quickstart/spa/angular2/02-user-profile if you can check this out to show user info the example use {{profile?.picture}}(for the picture) but I want to save all the info like given_name, family_name. I’m trying to use “profile” in the component.ts and it doesn’t work.

Ah, I think I understand. So, in the QuickStart, the steps are essentially:

  • In the AuthService (auth.service.ts) create a getProfile call that calls Auth0 and stores it on the service as a public property.
  • In the profile component, during ngOnInit, check if there’s a user profile on the AuthService and, if not, make that getProfile call and store the profile on the component (note that it will also be stored on the AuthService).
  • Then, display the profile on the template.

So, to save all of the profile to your backend, you’d most likely want to add that call in the callback function of the Auth0 userInfo call like this:

 this.auth0.client.userInfo(this._accessToken, (err, profile) => {
    if (profile) {
      self.userProfile = profile;
     // call API here to save profile and then handle accordingly
      this.myApiService.saveProfile(profile).subscribe((success) => success);
    }
    cb(err, profile);
  });
}

Of course, the details will depend on how your API is set up. But either way, you’ll have an API service that contains the actual POST call and then call that after you’ve received the profile from Auth0.

Does that help?

2 Likes