- Home
- Skills
- APIs & Backend
- keycloak multiple accounts
keycloak multiple accounts
by Dark inside
Enable multiple concurrent Keycloak accounts in SPAs using token parking and SSO cookie isolation.
$2
keycloak multiple accounts
Example session with this skill installed
I need to implement an account switcher in my React app using keycloak-js. Currently, trying to log in a second user just returns the first user. How do I architect this?
- Read your context and instructions
- Compiled the keycloak multiple accounts
To implement a multi-account switcher, you must decouple the active session tokens from the browser's SSO cookies. The following architecture uses a hybrid storage model and a proxy-level cookie clearing mechanism.
Multi-Account Architecture for Keycloak
| Component | Storage Type | Purpose |
|---|---|---|
| Active Account | sessionStorage | Holds kc_token and kc_refresh_token for the current tab. |
| Parked Accounts | localStorage | Map of sub to profile and refresh tokens for inactive accounts. |
| Cookie Proxy | Nginx/Caddy | The /forget-session endpoint to expire KEYCLOAK_IDENTITY cookies. |
Storage Implementation
// accounts.ts
export interface Account {
sub: string;
tokens: { token: string; refreshToken: string; idToken: string };
profile: any;
}
export const parkAccount = (sub: string, tokens: any, profile: any) => {
const accounts = JSON.parse(localStorage.getItem('kc_accounts') || '{}');
accounts[sub] = { sub, tokens, profile };
localStorage.setItem('kc_accounts', JSON.stringify(accounts));
};
export const getParkedAccount = (sub: string): Account | null => {
const accounts = JSON.parse(localStorage.getItem('kc_accounts') || '{}');
return accounts[sub] || null;
};
Nginx Proxy Configuration
location /realms/my-realm/forget-session {
add_header Set-Cookie "KEYCLOAK_SESSION=; Path=/realms/my-realm/; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Secure; SameSite=None";
add_header Set-Cookie "KEYCLOAK_IDENTITY=; Path=/realms/my-realm/; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Secure; SameSite=None";
# Add LEGACY cookies for older browser support
add_header Set-Cookie "KEYCLOAK_SESSION_LEGACY=; Path=/realms/my-realm/; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Secure; SameSite=None";
return 302 "$scheme://$http_host/realms/my-realm/protocol/openid-connect/auth?$query_string&prompt=login";
}
Next steps
- Update Keycloak Realm settings: set SSO Session Idle to at least 24 hours to keep parked accounts alive.
- Integrate
checkLoginIframe: falsein your Keycloak init options to prevent automatic logout loops. - Add the
/forget-sessionlocation to your Nginx or Traefik config.
Connects securely to your tools. The creator never sees your data.
About this skill
Managing multiple active sessions in a Single Page Application (SPA) using Keycloak is notoriously difficult due to HTTP-only SSO cookies. Standard OIDC libraries often fail when trying to add a second account, resulting in different_user_authenticated errors or silent redirects to the first user. This skill provides a proven architecture and implementation guide to enable account switching and "Add Account" functionality without killing existing sessions.
What it does
- Multi-account Storage manages the lifecycle of multiple tokens using a hybrid
sessionStorageandlocalStorageapproach. - SSO Cookie Management bypasses Keycloak cookie collisions via a custom
forget-sessionproxy pattern. - Session Switching performs silent token refreshes to swap active users without requiring new credentials.
- Per-account Logout revokes specific server-side sessions while keeping other parked accounts active.
- Session Lifespan Optimization provides configuration schemas for Keycloak realm settings to prevent premature token expiration.
How it works
- Initialize the OIDC client using tokens stored in
sessionStorageand disable the session-status iframe to prevent cookie-based logouts. - Add Account by parking current tokens in
localStorage, clearing the active tab session, and redirecting through a proxy endpoint that clears SSO cookies before showing the login form. - Switch Accounts by performing a
grant_type=refresh_tokenPOST request for a parked account and reloading the page with new tokens. - Clean Up by revoking individual refresh tokens via the OIDC logout endpoint when a user removes a specific account.
Frameworks & tools
This skill is framework-agnostic but includes implementation examples for keycloak-js. It requires a reverse proxy such as Nginx, Caddy, or Traefik to handle cookie clearing.
Why this beats prompting it yourself
Keycloak's default behavior is strictly one-session-per-browser-realm. Prompting usually leads to prompt=login suggestions that still trigger OIDC errors or keycloak.logout() calls that destroy all sessions. This skill provides the specific storage orchestration and proxy configuration needed to handle the underlying cookie constraints.
Use cases
- Adding an "Add another account" button to an enterprise SaaS dashboard.
- Building a fast account switcher for developers or admins managing multiple profiles.
- Resolving
different_user_authenticatederrors in multi-tenant SPAs.
Known limitations
Requires control over the reverse proxy or CDN worker to implement the cookie-clearing endpoint. Parked tokens are stored in localStorage, which requires a strict CSP to mitigate XSS risks.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed 17 days ago
Permissions
What's inside
Creator
Dark inside
Frequently Asked Questions
New in APIs & Backend
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for APIs & Backend.