Error Handling - With AutoLogin

In Auto Login mode, there is no “fallback” unauthenticated experience if oidc-spa fails to initialize, so error handling is different.

We expose separate hooks/components so you can handle initialization errors gracefully and render a clear, user-facing message while keeping diagnostics in the console for developers.

In autoLogin: false, createOidc never throws (if it does, that’s a bug). In autoLogin: true, createOidc may throw only an OidcInitializationError, which includes:

  • isAuthServerLikelyDown: helps you distinguish a temporary outage from a misconfiguration.

  • message: a developer-oriented diagnostic (do not show this to end users).

import { createOidc, type OidcInitializationError } from "oidc-spa/core";

const oidc = await createOidc({
    // ...
    autoLogin: true
})
// In autoLogin: false, createOidc never throws.
// In autoLogin: true, it can throw — but only OidcInitializationError —
// so you can safely narrow/cast here.
.catch(error => error as OidcInitializationError);

if( oidc instanceof Error ){

    const oidcInitializationError = oidc;
    
    // Use this to distinguish a misconfiguration from a temporary auth-server outage.
    // NOTE: below references should use `oidcInitializationError`.
    console.log(initializationError.isAuthServerLikelyDown);
    
    // Developer-only diagnostic with likely cause and fix.
    // Do not display this to end users.
    console.log(initializationError.message);
    
    alert("Our auth is down, sorry :(");
    
    // Halt the app in a typed-safe way (nothing renders until you decide otherwise).
    await Promise<never>(()=>{});
}

Last updated

Was this helpful?