provideAuth
provideAuth returns EnvironmentProviders that register the AuthService, its configuration, and an APP_INITIALIZER that automatically initializes the OIDC client during application bootstrap.
Installation
Section titled “Installation”npm install oidc-js-angularimport { bootstrapApplication } from "@angular/platform-browser";import { provideAuth } from "oidc-js-angular";import { AppComponent } from "./app.component";
bootstrapApplication(AppComponent, { providers: [ provideAuth({ config: { issuer: "https://auth.example.com", clientId: "my-app", redirectUri: "http://localhost:4200/callback", scopes: ["openid", "profile", "email", "offline_access"], postLogoutRedirectUri: "http://localhost:4200", }, }), ],});Options
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
config | OidcConfig | required | OIDC configuration (issuer, clientId, redirectUri, etc.) |
fetchProfile | boolean | true | Whether to fetch the UserInfo endpoint after login |
onLogin | (returnTo: string) => void | - | Called after successful login with the URL to restore |
onError | (error: Error) => void | - | Called when an error occurs during initialization |
Default navigation
Section titled “Default navigation”By default, after a successful login callback, the adapter uses Angular’s Router.navigateByUrl to restore the pre-login URL. You can override this with onLogin:
provideAuth({ config: { /* ... */ }, onLogin: (returnTo) => { // Custom navigation logic window.location.href = returnTo; },}),Lifecycle
Section titled “Lifecycle”- During bootstrap,
APP_INITIALIZERcallsAuthService.init() init()creates anOidcClientand fetches the OIDC discovery document- If the URL contains a
codeandstateparameter (callback from IdP), it exchanges the code for tokens - If the URL contains an
errorparameter, it callsonError - On destroy, the service unsubscribes and destroys the client via
DestroyRef