Auth0 allows you to add authentication to almost any application type quickly. This guide demonstrates how to integrate Auth0, add authentication, and display user profile information in any Angular application using the Auth0 Angular SDK.
To use this quickstart, you’ll need to:
To use Auth0 services, you’ll need to have an application set up in the Auth0 Dashboard. The Auth0 application is where you will configure how you want authentication to work for the project you are developing.
Use the interactive selector to create a new Auth0 application or select an existing application that represents the project you want to integrate with. Every application in Auth0 is assigned an alphanumeric, unique client ID that your application code will use to call Auth0 APIs through the SDK.
Any settings you configure using this quickstart will automatically update for your Application in the Dashboard, which is where you can manage your Applications in the future.
If you would rather explore a complete configuration, you can view a sample application instead.
A callback URL is a URL in your application that you would like Auth0 to redirect users to after they have authenticated. If not set, users will not be returned to your application after they log in.
A logout URL is a URL in your application that you would like Auth0 to redirect users to after they have logged out. If not set, users will not be able to log out from your application and will receive an error.
An Allowed Web Origin is a URL that you want to be allowed to access to your authentication flow. This must contain the URL of your project. If not properly set, your project will be unable to silently refresh authentication tokens, so your users will be logged out the next time they visit your application or refresh a page.
Auth0 provides an Angular SDK to simplify the process of implementing Auth0 authentication and authorization in Angular applications.
Install the Auth0 Angular SDK by running the following command in your terminal:
npm install @auth0/auth0-angular
Was this helpful?
The SDK exposes several types that help integrate Auth0 in an Angular application idiomatically, including a module and an authentication service.
The SDK exports
AuthModule, which is a module that contains all the services required for the SDK to function. To register this with your application:
AuthModuletype from the
AuthModuleto the application by calling
AuthModule.forRootand adding to your application module's
To configure the module, we use the
forRoot() pattern, which takes the properties
clientId; the values of these properties correspond to the Domain and Client ID values that you can find under Settings in the Single-Page Application (SPA) that you registered with Auth0.
Now that you have configured your Auth0 Application and the Auth0 Angular SDK, you need to set up login for your project. To do this, you will use the SDK’s
loginWithRedirect() method from the
AuthService class to redirect users to the Auth0 Universal Login page where Auth0 can authenticate them. After a user successfully authenticates, they will be redirected to your application and the callback URL you set up earlier in this quickstart.
Create a login button in your application that calls
loginWithRedirect() when selected.
You should now be able to log in to your application.
Run your application, and select the login button. Verify that:
Users who log in to your project will also need a way to log out. The SDK provides a
logout() method on the
AuthService class that you can use to log a user out of your app. When users log out, they will be redirected to your Auth0 logout endpoint, which will then immediately redirect them to your application and the logout URL you set up earlier in this quickstart.
Create a logout button in your application that calls
logout() when selected.
You should now be able to log out of your application.
Run your application, log in, and select the logout button. Verify that:
Now that your users can log in and log out, you will likely want to be able to retrieve the profile information associated with authenticated users. For example, you may want to be able to personalize the user interface by displaying a logged-in user’s name or profile picture.
The Auth0 Angular SDK provides user information through the
user$ observable exposed by the
AuthService class. Because the
user$ observable contains sensitive information and artifacts related to the user's identity, its availability depends on the user's authentication status. Fortunately, the
user$ observable is configured to only emit values once the
isAuthenticated$ observable is true, so there is no need to manually check the authentication state before accessing the user profile data.
The SDK also exposes an
isAuthenticated$ observable on the
AuthService class that allows you to check whether a user is authenticated or not, which you can use to determine whether to show or hide UI elements, for example.
UserProfileComponent code in the interactive panel to see examples of how to use these functions.
You should now be able to view user profile information.
Run your application, and verify that:
Excellent work! If you made it this far, you should now have login, logout, and user profile information running in your application.
This concludes our quickstart tutorial, but there is so much more to explore. To learn more about what you can do with Auth0, check out:
Did it work?
Any suggestion or typo?Edit on GitHub
Sign up for an or to your existing account to integrate directly with your own tenant.