A library for integrating Auth0 into an Angular application.
📚 Documentation - 🚀 Getting Started - 💻 API Reference - 💬 Feedback
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.
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
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
RS256and that "OIDC Conformant" is enabled.
Next, configure the following URLs for your application under the "Application URIs" section of the "Settings" page:
http://localhost:4200
http://localhost:4200
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.
The recommended approach is to use the functional
provideAuth0() in your application configuration:
import { ApplicationConfig } from '@angular/core';
import { provideAuth0 } from '@auth0/auth0-angular';
export const appConfig: ApplicationConfig = {
providers: [
provideAuth0({
domain: 'YOUR_AUTH0_DOMAIN',
clientId: 'YOUR_AUTH0_CLIENT_ID',
authorizationParams: {
redirect_uri: window.location.origin,
},
}),
],
};
If you're using NgModules, you can configure the SDK using
AuthModule.forRoot():
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 {}
Instead of providing static configuration, you can use
provideAppInitializer to load your config from an
external source before the SDK is instantiated, and configure it
using AuthClientConfig.set.
import { ApplicationConfig, inject, provideAppInitializer } from '@angular/core';
import { provideHttpClient, HttpBackend, HttpClient } from '@angular/common/http';
import { provideAuth0, AuthClientConfig } from '@auth0/auth0-angular';
export function configInitializer(handler: HttpBackend, config: AuthClientConfig) {
return () =>
new HttpClient(handler)
.get('/config')
.toPromise()
.then((loadedConfig: any) => config.set(loadedConfig));
}
export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient(),
provideAuth0(),
provideAppInitializer(() => {
const handler = inject(HttpBackend);
const config = inject(AuthClientConfig);
return configInitializer(handler, config)();
}),
],
};
Important: 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 why the dynamic configuration should be set using an app initializer, which ensures the configuration is available prior to instantiating the SDK.
:information_source: Any request made through an instance of
HttpClientthat got instantiated by Angular will use all configured interceptors, including ourAuthHttpInterceptor. Because theAuthHttpInterceptorrequires the existence of configuration settings, the request for retrieving those dynamic configuration settings should ensure it's not using any interceptors. In Angular, this can be done by manually instantiatingHttpClientusing an injectedHttpBackendinstance.
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.
import { APP_INITIALIZER } from '@angular/core';
import { HttpClientModule, HttpClient, HttpBackend } from '@angular/common/http';
import { AuthModule, AuthClientConfig } from '@auth0/auth0-angular';
function configInitializer(handler: HttpBackend, config: AuthClientConfig) {
return () =>
new HttpClient(handler)
.get('/config')
.toPromise()
.then((loadedConfig: any) => config.set(loadedConfig));
}
@NgModule({
imports: [HttpClientModule, AuthModule.forRoot()],
providers: [
{
provide: APP_INITIALIZER,
useFactory: configInitializer,
deps: [HttpBackend, AuthClientConfig],
multi: true,
},
],
})
export class AppModule {}
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.
Explore public API's available in auth0-angular.
We appreciate feedback and contribution to this repo! Before you get started, please see the following:
To provide feedback or report a bug, please raise an issue on our issue tracker.
Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
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.