The provideKeycloak
function simplifies the initialization and integration of Keycloak with Angular applications. It ensures that Keycloak is properly configured and injected into the Angular Dependency Injection (DI) system, making it accessible throughout the application.
The provideKeycloak
function configures and initializes a Keycloak instance at the application startup. It allows developers to easily set up Keycloak features, interceptors, and additional providers required for authentication and authorization.
- Seamless Integration: Initializes Keycloak and injects it into the Angular DI system.
- Support for Keycloak Features: Enables modular features like automatic token refresh and session management.
- Flexible Initialization: Allows custom initialization logic by skipping automatic initialization.
export function provideKeycloak(options: ProvideKeycloakOptions): EnvironmentProviders;
Parameter | Type | Description |
---|---|---|
options |
ProvideKeycloakOptions |
Configuration options for Keycloak, including its instance, initialization settings, and additional providers. |
export type ProvideKeycloakOptions = {
config: KeycloakConfig; // Keycloak server configuration
initOptions?: KeycloakInitOptions; // Optional initialization options
providers?: Array<Provider | EnvironmentProviders>; // Additional Angular providers
features?: Array<KeycloakFeature>; // Array of Keycloak features
};
-
config
:
Keycloak server configuration, including:url
: Keycloak server URL.realm
: The realm to authenticate against.clientId
: The client ID for your application.
-
initOptions
:
Initialization options for the Keycloak instance, such as:onLoad
: Determines whether to check SSO or require login ('check-sso'
or'login-required'
).silentCheckSsoRedirectUri
: URL for the iframe used in silent SSO.
-
providers
:
Additional Angular providers required by your application. -
features
:
Keycloak features such aswithAutoRefreshToken
to enhance functionality.
import { provideKeycloak } from 'keycloak-angular';
import { ApplicationConfig } from '@angular/core';
export const appConfig: ApplicationConfig = {
providers: [
provideKeycloak({
config: {
url: 'https://auth-server.example.com',
realm: 'my-realm',
clientId: 'my-client'
},
initOptions: {
onLoad: 'login-required',
silentCheckSsoRedirectUri: `${window.location.origin}/silent-check-sso.html`
}
})
]
};
To enable advanced functionality like automatic token refresh, include the features
option.
import { provideKeycloak, withAutoRefreshToken, AutoRefreshTokenService, UserActivityService } from 'keycloak-angular';
export const appConfig = {
providers: [
provideKeycloak({
config: {
url: 'https://auth-server.example.com',
realm: 'my-realm',
clientId: 'my-client'
},
initOptions: {
onLoad: 'check-sso',
silentCheckSsoRedirectUri: `${window.location.origin}/silent-check-sso.html`
},
features: [
withAutoRefreshToken({
sessionTimeout: 300000, // 5 minutes
onInactivityTimeout: 'logout'
})
],
providers: [AutoRefreshTokenService, UserActivityService]
})
]
};
If you need to control when keycloak.init()
is called, omit the initOptions
parameter. This gives you full control over the initialization process.