keycloak multiple accounts

    by Dark inside

    1

    Enable multiple concurrent Keycloak accounts in SPAs using token parking and SSO cookie isolation.

    $2

    Secure checkout via Stripe

    Money-back guarantee

    Converts to your local currency at checkout

    Security scanned

    Works with the AI Agents you already use

    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

    ComponentStorage TypePurpose
    Active AccountsessionStorageHolds kc_token and kc_refresh_token for the current tab.
    Parked AccountslocalStorageMap of sub to profile and refresh tokens for inactive accounts.
    Cookie ProxyNginx/CaddyThe /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

    1. Update Keycloak Realm settings: set SSO Session Idle to at least 24 hours to keep parked accounts alive.
    2. Integrate checkLoginIframe: false in your Keycloak init options to prevent automatic logout loops.
    3. Add the /forget-session location 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 sessionStorage and localStorage approach.
    • SSO Cookie Management bypasses Keycloak cookie collisions via a custom forget-session proxy 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

    1. Initialize the OIDC client using tokens stored in sessionStorage and disable the session-status iframe to prevent cookie-based logouts.
    2. 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.
    3. Switch Accounts by performing a grant_type=refresh_token POST request for a parked account and reloading the page with new tokens.
    4. 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_authenticated errors 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. 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. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 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

    Terminal / Shell
    Network Access
    Listed17 days ago

    What's inside

    Creator

    Dark inside

    Dark inside

    Frequently Asked Questions

    Standard OIDC implementations rely on a single SSO cookie that prevents adding a second user; this skill provides a specialized architecture to isolate those cookies and manage multiple token sets simultaneously without session collisions.

    The purchase includes a comprehensive implementation guide, the storage orchestration logic for token parking, and the specific reverse proxy configurations (Nginx/Caddy) required to handle cookie isolation.

    Yes, while the examples focus on keycloak-js, the architectural pattern and proxy-level configurations are compatible with any OIDC-compliant frontend framework including React, Vue, and Angular.

    This skill is designed for AI agents that assist in software architecture and development, providing them with the specific technical blueprint needed to solve complex authentication workflows that generic LLMs typically struggle with.

    It requires a reverse proxy or CDN worker (like Nginx or Cloudflare Workers) to intercept and clear specific cookies, as client-side JavaScript cannot bypass HTTP-only cookie restrictions on its own.

    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.