Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Auth0 SDK for Angular Single Page Applications

License

NotificationsYou must be signed in to change notification settings

auth0/auth0-angular

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

593 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Auth0 SDK for Angular Single Page Applications

A library for integratingAuth0 into an Angular application.

ReleaseCodecovAsk DeepWikiDownloadsLicenseCircleCI

📚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 theactively 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 haveng-add support, so the library can also be installed using the Angular CLI:

ng add @auth0/auth0-angular

Configure Auth0

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 toRS256 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 theClient ID andDomain values under the "Basic Information" section. You'll need these values in the next step.

Configure the SDK

Static configuration

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{}

Dynamic configuration

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 ofHttpClient that got instantiated by Angular will use all 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 interceptors. In Angular, this can be done by manually instantiatingHttpClient using an injectedHttpBackend instance.

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{}

Add login to your application

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.

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, pleaseraise 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.


Auth0 Logo

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.


[8]ページ先頭

©2009-2026 Movatter.jp