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:
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 PromisefunctionconfigInitializer(
handler: HttpBackend,
config: AuthClientConfig
) {
return() =>newHttpClient(handler)
.get('/config')
.toPromise()
// Set the config that was loaded asynchronously here
.then((loadedConfig: any) => config.set(loadedConfig));
}
exportclassAppModule {
// ...imports: [
HttpClientModule,
AuthModule.forRoot(), // <- don't pass any config here
],
providers: [
{
provide: APP_INITIALIZER,
useFactory: configInitializer, // <- pass your initializer function heredeps: [HttpBackend, AuthClientConfig],
multi: true,
},
],
// ...
}
Add login to your application
To log the user into the application, inject the AuthService and call its loginWithRedirect method.
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.
AuthService - service used to interact with the SDK.
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.
Auth0 SDK for Angular Single Page Applications (SPA)
We found that @auth0/auth0-angular demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 45 open source maintainers collaborating on the project.
Package last updated on 16 Jan 2024
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.