Add Login to Your Angular App

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:

  • Sign up for a free Auth0 account or log in to Auth0.
  • Have a working Angular project that you want to integrate with. Alternatively, you can view or download a sample application after logging in.

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.

Configure an application

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.

Configure Callback URLs

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.

Configure Logout URLs

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.

Configure Allowed Web Origins

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:

  1. Open the app.module.ts file.
  2. Import the AuthModule type from the @auth0/auth0-angular package.
  3. Add AuthModule to the application by calling AuthModule.forRoot and adding to your application module's imports array.
  4. Inject AuthService into AppComponent.

To configure the module, we use the forRoot() pattern, which takes the properties domain and 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. On top of that, we configure authorizationParams.redirect_uri, which allows Auth0 to redirect the user back to the specific URL after successfully authenticating.

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.

Checkpoint

You should now be able to log in to your application.

Run your application, and select the login button. Verify that:

  • you can log in or sign up using a username and password.
  • your application redirects you to the Auth0 Universal Login page.
  • you are redirected to Auth0 for authentication.
  • Auth0 successfully redirects back to your application after authentication.
  • you do not receive any errors in the console related to Auth0.

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.

Checkpoint

You should now be able to log out of your application.

Run your application, log in, and select the logout button. Verify that:

  • you are redirected to Auth0's logout endpoint.
  • Auth0 successfully redirects back to your application and the correct logout URL.
  • you are no longer logged in to your application.
  • you do not receive any errors in the console related to Auth0.

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.

Review the UserProfileComponent code in the interactive panel to see examples of how to use these functions.

Checkpoint

You should now be able to view user profile information.

Run your application, and verify that:

  • user information displays correctly after you have logged in.
  • user information does not display after you have logged out.

Next Steps

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

Sign up for an or to your existing account to integrate directly with your own tenant.