- Notifications
You must be signed in to change notification settings - Fork70
Auth0 SDK for Angular Single Page Applications
License
auth0/auth0-angular
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A library for integratingAuth0 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 theactively 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 haveng-add support, so the library can also be installed using the Angular CLI:
ng add @auth0/auth0-angular
Create aSingle Page Application in theAuth0 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:
- 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 theClient ID andDomain values under the "Basic Information" section. You'll need these values in the next step.
The recommended approach is to use the functionalprovideAuth0() in your application configuration:
import{ApplicationConfig}from'@angular/core';import{provideAuth0}from'@auth0/auth0-angular';exportconstappConfig:ApplicationConfig={providers:[provideAuth0({domain:'YOUR_AUTH0_DOMAIN',clientId:'YOUR_AUTH0_CLIENT_ID',authorizationParams:{redirect_uri:window.location.origin,},}),],};
Using NgModules
If you're using NgModules, you can configure the SDK usingAuthModule.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,},}),],})exportclassAppModule{}
Instead of providing static configuration, you can useprovideAppInitializer to load your config from an external source before the SDK is instantiated, and configure it usingAuthClientConfig.set.
import{ApplicationConfig,inject,provideAppInitializer}from'@angular/core';import{provideHttpClient,HttpBackend,HttpClient}from'@angular/common/http';import{provideAuth0,AuthClientConfig}from'@auth0/auth0-angular';exportfunctionconfigInitializer(handler:HttpBackend,config:AuthClientConfig){return()=>newHttpClient(handler).get('/config').toPromise().then((loadedConfig:any)=>config.set(loadedConfig));}exportconstappConfig:ApplicationConfig={providers:[provideHttpClient(),provideAuth0(),provideAppInitializer(()=>{consthandler=inject(HttpBackend);constconfig=inject(AuthClientConfig);returnconfigInitializer(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.
ℹ️ 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.
Using NgModules
Instead of usingAuthModule.forRoot to specify auth configuration, you can provide a factory function usingAPP_INITIALIZER to load your config from an external source before the auth module is loaded, and provide your configuration usingAuthClientConfig.set.
import{APP_INITIALIZER}from'@angular/core';import{HttpClientModule,HttpClient,HttpBackend}from'@angular/common/http';import{AuthModule,AuthClientConfig}from'@auth0/auth0-angular';functionconfigInitializer(handler:HttpBackend,config:AuthClientConfig){return()=>newHttpClient(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,},],})exportclassAppModule{}
To log the user into the application, inject theAuthService and call itsloginWithRedirect method.
import{Component}from'@angular/core';import{AuthService}from'@auth0/auth0-angular';@Component({selector:'app-root',templateUrl:'./app.component.html',styleUrls:['./app.component.css'],})exportclassAppComponent{constructor(publicauth: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 theredirectUri option.
For more code samples on how to integrate theauth0-angular SDK in yourAngular application, including how to use our standalone and function APIs, have a look at theexamples.
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, pleaseraise 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 checkoutWhy Auth0?
This project is licensed under the MIT license. See the LICENSE file for more info.
About
Auth0 SDK for Angular Single Page Applications
Topics
Resources
License
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.

