Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@auth0/auth0-angular

auth0271.7kMIT2.2.3TypeScript support: included

Auth0 SDK for Angular Single Page Applications (SPA)

auth0, login, Authorization Code Grant Flow, PKCE, Single Page Application authentication, SPA authentication, angular

readme

Auth0 SDK for Angular Single Page Applications

A library for integrating Auth0 into an Angular application.

Release Codecov Downloads License CircleCI

📚 Documentation - 🚀 Getting Started - 💻 API Reference - 💬 Feedback

Documentation

  • Quickstart - our interactive guide for quickly adding login, logout and user information to an Angular app using Auth0.
  • Sample App - a full-fledged Angular application integrated with Auth0.
  • FAQs - frequently asked questions about the auth0-angular SDK.
  • Examples - code samples for common Angular authentication scenario's.
  • Docs site - explore our docs site and learn more about Auth0.

Getting started

Requirements

This project only supports the actively supported versions of Angular as stated in the Angular documentation. Whilst other versions might be compatible they are not actively supported.

Installation

Using npm:

npm install @auth0/auth0-angular

We also have ng-add support, so the library can also be installed using the Angular CLI:

ng add @auth0/auth0-angular

Configure Auth0

Create a Single Page Application in the Auth0 Dashboard.

If you're using an existing application, verify that you have configured the following settings in your Single Page Application:

  • Click on the "Settings" tab of your application's page.
  • Scroll down and click on the "Show Advanced Settings" link.
  • Under "Advanced Settings", click on the "OAuth" tab.
  • Ensure that "JsonWebToken Signature Algorithm" is set to RS256 and that "OIDC Conformant" is enabled.

Next, configure the following URLs for your application under the "Application URIs" section of the "Settings" page:

  • Allowed Callback URLs: http://localhost:4200
  • Allowed Logout URLs: http://localhost:4200
  • Allowed Web Origins: http://localhost:4200

These URLs should reflect the origins that your application is running on. Allowed Callback URLs may also include a path, depending on where you're handling the callback.

Take note of the Client ID and Domain values under the "Basic Information" section. You'll need these values in the next step.

Configure the SDK

Static configuration

Install the SDK into your application by importing AuthModule.forRoot() and configuring with your Auth0 domain and client id, as well as the URL to which Auth0 should redirect back after succesful authentication:

import { NgModule } from '@angular/core';
import { AuthModule } from '@auth0/auth0-angular';

@NgModule({
  // ...
  imports: [
    AuthModule.forRoot({
      domain: 'YOUR_AUTH0_DOMAIN',
      clientId: 'YOUR_AUTH0_CLIENT_ID',
      authorizationParams: {
        redirect_uri: window.location.origin,
      },
    }),
  ],
  // ...
})
export class AppModule {}

Dynamic configuration

Instead of using AuthModule.forRoot to specify auth configuration, you can provide a factory function using APP_INITIALIZER to load your config from an external source before the auth module is loaded, and provide your configuration using AuthClientConfig.set.

The configuration will only be used initially when the SDK is instantiated. Any changes made to the configuration at a later moment in time will have no effect on the default options used when calling the SDK's methods. This is also the reason why the dynamic configuration should be set using an APP_INITIALIZER, because doing so ensures the configuration is available prior to instantiating the SDK.

:information_source: Any request made through an instance of HttpClient that got instantiated by Angular, will use all of the configured interceptors, including our AuthHttpInterceptor. Because the AuthHttpInterceptor requires the existence of configuration settings, the request for retrieving those dynamic configuration settings should ensure it's not using any of those interceptors. In Angular, this can be done by manually instantiating HttpClient using an injected HttpBackend instance.

import { AuthModule, AuthClientConfig } from '@auth0/auth0-angular';

// Provide an initializer function that returns a Promise
function configInitializer(
  handler: HttpBackend,
  config: AuthClientConfig
) {
  return () =>
    new HttpClient(handler)
      .get('/config')
      .toPromise()
      // Set the config that was loaded asynchronously here
      .then((loadedConfig: any) => config.set(loadedConfig));
}

export class AppModule {
  // ...
  imports: [
    HttpClientModule,
    AuthModule.forRoot(), // <- don't pass any config here
  ],
  providers: [
    {
      provide: APP_INITIALIZER,
      useFactory: configInitializer, // <- pass your initializer function here
      deps: [HttpBackend, AuthClientConfig],
      multi: true,
    },
  ],
  // ...
}

Add login to your application

To log the user into the application, inject the AuthService and call its loginWithRedirect method.

import { Component } from '@angular/core';
import { AuthService } from '@auth0/auth0-angular';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  constructor(public auth: AuthService) {}

  loginWithRedirect() {
    this.auth.loginWithRedirect();
  }

By default the application will ask Auth0 to redirect back to the root URL of your application after authentication. This can be configured by setting the redirectUri option.

For more code samples on how to integrate the auth0-angular SDK in your Angular application, including how to use our standalone and function APIs, have a look at the examples.

API reference

Explore public API's available in auth0-angular.

Feedback

Contributing

We appreciate feedback and contribution to this repo! Before you get started, please see the following:

Raise an issue

To provide feedback or report a bug, please raise an issue on our issue tracker.

Vulnerability Reporting

Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.


<picture> <source media="(prefers-color-scheme: light)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150"> <source media="(prefers-color-scheme: dark)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_dark_mode.png" width="150"> Auth0 Logo </picture>

Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0?

This project is licensed under the MIT license. See the LICENSE file for more info.

changelog

Change Log

v2.2.3 (2024-01-15)

Full Changelog

Changed

v2.2.2 (2023-12-12)

Full Changelog

Changed

v2.2.1 (2023-07-24)

Full Changelog

Fixed

v2.2.0 (2023-07-13)

Full Changelog

Added

v2.1.0 (2023-04-03)

Full Changelog

Added

Fixed

v2.0.2 (2023-03-10)

Full Changelog

Fixed

v2.0.1 (2023-01-25)

Full Changelog

Fixed

v2.0.0 (2023-01-19)

Full Changelog

Auth0-Angular v2 includes many significant changes compared to v1:

  • Remove polyfills from bundles
  • Introduce authorizationParams and logoutParams to hold properties sent to Auth0
  • Remove buildAuthorizeUrl and buildLogoutUrl
  • Remove redirectMethod, and replace by openUrl
  • Remove localOnly from logout in favor of openUrl
  • Ensure logout returns an Onservable instead of a Promise
  • Rework ignoreCache to cacheMode and introduce cache-only
  • Use form-encoded data by default
  • Do not fallback to refreshing tokens via iframe method by default
  • getUser and getIdTokenClaims are removed
  • Remove advancedOptions.defaultScope and replace with scope

As with any major version bump, v2 of Auth0-Angular contains a set of breaking changes. Please review the migration guide thoroughly to understand the changes required to migrate your application to v2.

v2.0.0-beta.0 (2022-12-13)

Full Changelog

Auth0-Angular v2 includes many significant changes compared to v1:

  • Remove polyfills from bundles
  • Introduce authorizationParams and logoutParams to hold properties sent to Auth0
  • Remove buildAuthorizeUrl and buildLogoutUrl
  • Remove redirectMethod, and replace by openUrl
  • Remove localOnly from logout in favor of openUrl
  • Ensure logout returns an Onservable instead of a Promise
  • Rework ignoreCache to cacheMode and introduce cache-only
  • Use form-encoded data by default
  • Do not fallback to refreshing tokens via iframe method by default
  • getUser and getIdTokenClaims are removed
  • Remove advancedOptions.defaultScope and replace with scope

As with any major version bump, v2 of Auth0-Angular contains a set of breaking changes. Please review the migration guide thoroughly to understand the changes required to migrate your application to v2.

v1.11.1 (2022-11-17)

Full Changelog

Changed

v1.11.0 (2022-10-03)

Full Changelog

Changed

Note: This release drops support for Angular <12 as those versions are no longer supported by Google themselves. [Read more ...]

v1.10.1 (2022-07-26)

Full Changelog

Fixed

v1.10.0 (2022-06-08)

Full Changelog

Added

Changed

v1.9.0 (2022-02-14)

Full Changelog

Added

v1.8.2 (2022-01-11)

Full Changelog

Fixed

v1.8.1 (2021-12-07)

Full Changelog

Changed

Fixed

v1.8.0 (2021-10-27)

Full Changelog

Added

v1.7.0 (2021-09-20)

Full Changelog

Added

v1.6.2 (2021-09-15)

Full Changelog

Fixed

v1.6.1 (2021-09-10)

Full Changelog

Fixed

v1.6.0 (2021-07-14)

Full Changelog

Added

Fixed

  • [SDK-2635] Avoid emitting error when calling endpoints using allowAnonymous #180 (frederikprijck)

v1.5.1 (2021-05-25)

Full Changelog

Changed

v1.5.0 (2021-05-06)

Full Changelog

Added

v1.4.1 (2021-04-13)

Full Changelog

Added

Changed

  • Remove unnecessary returns from tap #141 (sorohan)
  • Only emit user$ and idTokenClaims$ when login, logout or token changed #131 (frederikprijck)

v1.4.0 (2021-03-25)

Full Changelog

Added

Changed

v1.3.2 (2021-01-21)

Full Changelog

Changed

Fixed

v1.3.1 (2020-12-18)

Full Changelog

Changed

Fixed

Security

v1.3.0 (2020-12-03)

Full Changelog

Added

Changed

Fixed

  • [SDK-2140] Emit null for user and idTokenClaims when logged out #90 (frederikprijck)

Security

v1.2.0 (2020-10-22)

Full Changelog

Added

Fixed

v1.1.0 (2020-10-14)

Full Changelog

Added

  • [SDK-1976] Dynamic configuration (alternative to AuthModule.forRoot) #62 (stevehobbsdev)

v1.0.0 (2020-09-16)

Full Changelog

Added

Changed

  • [SDK-1895] Add options to match HTTP requests based on HTTP method #48 (stevehobbsdev)
  • Update Readme with more badges and rewording of expectations #40 (stevehobbsdev)

Removed

Fixed

  • docs: add return type to the example in the README #44 (ahasall)
  • [SDK-1923] Improve readability of Calling an API section in the readme #42 (stevehobbsdev)
  • Fix publish script using the new release version #39 (lbalmaceda)

v0.2.1 (2020-08-14)

Full Changelog

Changed

v0.2.0 (2020-08-14)

Full Changelog

Added

Removed

v0.1.0 (2020-07-31)

Early Access Release Do not use it in a Production environment.

Installation

In order to install this package on your application, download the auth0-auth0-angular-0.1.0.tgz file from the Releases section on the Github repository and run the following command:

npm install /path/to/auth0-auth0-angular-0.1.0.tgz

Added

Fixed