githubEdit

arrow-up-to-dotted-lineMigrating from Keycloak-js

Polyfilling keycloak-js with oidc-spa

If you're using keycloak-jsarrow-up-right in an existing codebase, you can migrate to oidc-spa without a painful rewrite. oidc-spa ships a keycloak-js polyfill. It’s a literal drop-in replacement.

chevron-rightWhy switch?hashtag

Security

UX

  • Auto Logout: optional “You will be logged out in 30…29…” overlay. No more “submit → redirect to login” because the Keycloak session expired.

  • Login/Logout propagation across tabs.

  • Much faster and relyable SSO, especially in non ideal condition (iframe blocked / Keycloak not on same site, slow network...)

1

Update dependency

Replace keycloak-js with oidc-spa in your package.json.

package.json
 {
     dependencies: {
-        "keycloak-js": "...",
+        "oidc-spa": "..."
     }
 }
2

Update your codebase

-import Keycloak from "keycloak-js";
+import { Keycloak } from "oidc-spa/keycloak-js";

 // ...

 await keycloak.init({
     onLoad: 'check-sso',
-    silentCheckSsoRedirectUri: `${location.origin}/silent-check-sso.html`,
     // ...
 });

Delete public/silent-check-sso.html.

3

(OPTIONAL) Fix your Valid Redirect URIs

Log in to the Keycloak Admin Console. Open your client configuration.

Valid Redirect URIs:
 http://localhost*
-https://dashboard.my-company.com/*
-https://dashboard.my-company.com/silent-check-sso.html
+https://dashboard.my-company.com/
4

Enable Security Features

If you're moving to oidc-spa, you likely want to enable DPoP and other security features.

Pick the setup option that best fits your project:

If you're in a Vite project, the recommended approach is to use oidc-spa’s Vite plugin.

vite.config.ts
import { defineConfig } from "vite";
import { oidcSpa } from "oidc-spa/vite-plugin";

export default defineConfig({
    plugins: [
        // ...
        oidcSpa({
            // See: https://docs.oidc-spa.dev/v/v10/security-features/browser-runtime-freeze
            browserRuntimeFreeze: {
                enabled: true
                //exclude: [ "fetch", "XMLHttpRequest"]
            },
            // See: https://docs.oidc-spa.dev/v/v10/security-features/dpop
            DPoP: {
                enabled: true,
                mode: "auto"
            }
        })
    ]
});

You can enable keycloak.init({ enableLogging: true }) to see a console report for the security features.

5

(OPTIONAL) Display a Warning Before Auto Logout

oidc-spa implements auto logout by respecting the idle session lifetime you configured in Keycloak.

To warn the user when they are about to be logged out due to inactivity, you can show an overlay like: “Are you still here? Your session will expire in 30…29…”

Get the underlying oidc-spa core object like this:

import { Keycloak } from "oidc-spa/keycloak-js";

const keycloak = new Keycloak({ ... });

await keycloak.init({ ... });

// Can call this only after keycloak.init() has resolved.
const oidc = keycloak.getOidc();

Then implement the overlay as described here: Displaying a Warning Before Auto Logout.

6

You're Done 🎉

If you run into some issue do not hesitate to reach out on Discordarrow-up-right.

Last updated

Was this helpful?