@auth-strategy-manager/keycloak
v2.0.1
Published
Keycloak strategy for auth-strategy-manager
Maintainers
Readme
@auth-strategy-manager/keycloak
Keycloak strategy for auth-strategy-manager. v2 targets @auth-strategy-manager/core ^2.0.0.
Documentation in other languages
- Russian (Русский)
- English (this file)
Installation
npm install @auth-strategy-manager/keycloak @auth-strategy-manager/core keycloak-jsUsage
Use AuthStrategyManager from core for persistence (AuthStorageManager, strategy name, tokens). KeycloakStrategy talks to Keycloak JS and returns AuthManagerData from checkAuth, signIn, and refreshToken.
import { AuthStrategyManager } from '@auth-strategy-manager/core';
import { KeycloakStrategy } from '@auth-strategy-manager/keycloak';
const keycloakStrategy = new KeycloakStrategy({
keycloak: {
realm: 'my-realm',
url: 'https://keycloak.example.com',
clientId: 'my-client',
},
signInUrl: 'https://myapp.com/login',
name: 'my-keycloak',
only: false,
init: {
flow: 'standard',
onLoad: 'check-sso',
},
});
const authManager = new AuthStrategyManager([keycloakStrategy]);
// Optional: align redirect base with the manager (used by keycloak.login / logout redirects)
keycloakStrategy.startUrl = authManager.startUrl ?? window.location.origin;
const state = await authManager.checkAuth();
await authManager.signIn();
await authManager.refreshToken(30);
await authManager.signOut();Before you test (integration checklist)
- Install
@auth-strategy-manager/core@^2.0.0together with@auth-strategy-manager/keycloak@^2.0.0(peer dependency). - Prefer
authManager.checkAuth()/signIn()/refreshToken()/signOut()soAuthStorageManagerstays in sync withAuthManagerData. Calling onlykeycloakStrategy.*skips manager persistence. keycloak.login()often triggers a full redirect; when the app loads again, runauthManager.checkAuth()(or your bootstrap flow) so tokens from Keycloak JS are written through the manager.- Set
keycloakStrategy.startUrl(andsignInUrlin config when needed) to URLs Keycloak is allowed to redirect to (must match Keycloak client settings). - With multiple strategies, call
authManager.use('my-keycloak')(same string asnamein config, default is'keycloak'). - There is no
KeycloakStrategy.clear()— useauthManager.clear(); tokens are not mirrored into Storage by this package.
Breaking changes from v1
checkAuthreturnsPromise<AuthManagerData>(notboolean). PreferauthManager.checkAuth()so storage stays in sync.refreshTokenreturnsPromise<AuthManagerData>(notvoid).- Strategy name and session flags are owned by core
AuthStrategyManager/AuthStorageManager. startUrlis stored on the strategy instance only (not mirrored vialocalStoragefrom this package).accessTokenstorage config removed — use coreAuthStorageManageronly; no duplicate mirror insessionStorage/localStoragefrom this package.- Peer dependency: install
@auth-strategy-manager/core^2.0.0 yourself.
Configuration
KeycloakConfig
import type { KeycloakInitOptions } from 'keycloak-js';
type KeycloakConfig = {
keycloak: {
realm: string;
url: string;
clientId: string;
};
init?: KeycloakInitOptions;
signInUrl?: string;
name?: string;
only?: boolean;
};Parameters
keycloak.*— Keycloak client settings passed tokeycloak-js.init— Options forkeycloak.init(default:{ flow: 'standard', onLoad: 'check-sso' }).signInUrl— Redirect URI for login / logout whenonlyis false.name— Strategy id (default:'keycloak').only— Iftrue,logoutuses noredirectUri.
Token storage is not configured on this strategy: use AuthStorageManager on AuthStrategyManager from core (Keycloak keeps tokens in memory on the JS client; checkAuth / refreshToken / signIn expose them via AuthManagerData for the manager to persist).
API
KeycloakStrategy
Constructor
constructor(config: KeycloakConfig)Methods
checkAuth(): Promise<AuthManagerData>signIn<T, D>(config?: D): Promise<T>— merges Keycloak session intoAuthManagerDatashape in the return value when no full page redirect occurs.signUp<T, D>(config?: D): Promise<T>— returns an unauthenticatedAuthManagerDataplaceholder (no Keycloak registration flow here).signOut(): Promise<void>—keycloak.logout.refreshToken<T>(sec?: T): Promise<AuthManagerData>—updateToken;secas a number setsminValiditySeconds(default 5).
Properties
name,keycloak,only,init,signInUrltoken(read-only) /isAuthenticated— from the Keycloak JS clientstartUrl— get/set in-memory redirect base
License
ISC
