A library for integrating Auth0 into an Angular application.
📚 Documentation - 🚀 Getting Started - 💻 API Reference - 💬 Feedback
- 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.
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.
- Ensure that "Token Endpoint Authentication Method" under "Application Properties" is set to "None"
- 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.
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 {}
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.
ℹ️ Any request made through an instance of
HttpClient
that got instantiated by Angular, will use all of the configured interceptors, including ourAuthHttpInterceptor
. Because theAuthHttpInterceptor
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 instantiatingHttpClient
using an injectedHttpBackend
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,
},
],
// ...
}
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, have a look at our examples.
Explore public API's available in auth0-angular.
- AuthService - service used to interact with the SDK.
- AuthConfig - used to configure the SDK.
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.