Skip to content

provideAuth

provideAuth returns EnvironmentProviders that register the AuthService, its configuration, and an APP_INITIALIZER that automatically initializes the OIDC client during application bootstrap.

Terminal window
npm install oidc-js-angular
import { 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",
},
}),
],
});
OptionTypeDefaultDescription
configOidcConfigrequiredOIDC configuration (issuer, clientId, redirectUri, etc.)
fetchProfilebooleantrueWhether 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

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;
},
}),
  1. During bootstrap, APP_INITIALIZER calls AuthService.init()
  2. init() creates an OidcClient and fetches the OIDC discovery document
  3. If the URL contains a code and state parameter (callback from IdP), it exchanges the code for tokens
  4. If the URL contains an error parameter, it calls onError
  5. On destroy, the service unsubscribes and destroys the client via DestroyRef