Angular 2+ User Profile

Sample Project

Download a sample project specific to this tutorial configured with your Auth0 API Keys.

System Requirements
  • Angular 2+
Show requirements

Most applications display profile information to authenticated users. Auth0 provides a /userinfo endpoint for that. When you send an access token to the endpoint, it returns a JSON object with information about the user. The information stored in that object depends on how the user logged in to your application.

To learn more about the information returned by the /userinfo endpoint, see the /userinfo endpoint documentation.

Request the Profile Scope

To retrieve user information, request a scope of openid profile in the instance of the auth0.WebAuth object.

// src/app/auth/auth.service.ts

auth0 = new auth0.WebAuth({
  // ...
  scope: 'openid profile'
});

Make a Call for the User's Information

Use the client.userInfo method from the auth0.js library to get user information from the /userinfo endpoint.

Use the following arguments in the client.userInfo method:

  1. The user's access_token
  2. A callback URL with arguments for a potential error and a profile

Add a method that calls the client.userInfo method to the AuthService service.

// src/app/auth/auth.service.ts

// ...
userProfile: any;

//...
public getProfile(cb): void {
  const accessToken = localStorage.getItem('access_token');
  if (!accessToken) {
    throw new Error('Access token must exist to fetch profile');
  }

  const self = this;
  this.auth0.client.userInfo(accessToken, (err, profile) => {
    if (profile) {
      self.userProfile = profile;
    }
    cb(err, profile);
  });
}

The service includes a local userProfile member that caches the profile information you requested with the getProfile call.

Add a Profile Component

Some applications have a dedicated profile section for displaying user information. The example below shows how to set it up.

Create a new component called ProfileComponent.

// src/app/profile/profile.component.ts

import { Component, OnInit } from '@angular/core';
import { AuthService } from './../auth/auth.service';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {

  profile: any;

  constructor(public auth: AuthService) { }

  ngOnInit() {
    if (this.auth.userProfile) {
      this.profile = this.auth.userProfile;
    } else {
      this.auth.getProfile((err, profile) => {
        this.profile = profile;
      });
    }
  }

}

The component injects the AuthService service. Then, it checks the service for a populated userProfile object. If it doesn't find the object, the component makes a call to getProfile to get the user's information.

The user's information can be displayed in a template.

<!-- src/app/profile/profile.component.html -->

<div class="panel panel-default profile-area">
  <div class="panel-heading">
    <h3>Profile</h3>
  </div>
  <div class="panel-body">
    <img src="{{profile?.picture}}" class="avatar" alt="avatar">
    <div>
      <label><i class="glyphicon glyphicon-user"></i> Nickname</label>
      <h3 class="nickname">{{ profile?.nickname }}</h3>
    </div>
    <pre class="full-profile">{{ profile | json }}</pre>
  </div>
</div>

Allow Users to Update Their Profile

You can allow your users to update their profile information. Auth0 provides a user_metadata object to store information that users can edit. See the metadata documentation for more information.

Previous Tutorial
1. Login
Next Tutorial
3. Calling an API
Use Auth0 for FREECreate free Account