Auto Login

Enforce authentiaction everywhere on your app

If there is no part of your app that can be browsed without being logged which is typically the case for application like dashboard or administration pannel, you can make oidc-spa automatically redirect users to the login pages when they are not authenticated.

In this mode you don't have to:

  • Check isUserLoggedIn, it will always be true.

  • (React) Use useOidc({ assert: "user logged in" }) you know that's the case.

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

const oidc = await createOidc({
    // ...
    autoLogin: true,
    // Optional, the default value is: location.href (here)
    // postLoginRedirectUrl: "/dashboard"
})
.catch((error: OidcInitializationError) => {
    // Here you need to handle the potential initialization error
    // because in this mode we cannot fallback to the user being
    // not authenticated if we ran into an error.

    if( !error.isAuthServerLikelyDown ){
        // This mean that there is an issue in the configuration
        // of your OIDC server.
        throw error;
    }

    alert("Sorry our authentication server is down, try again later");

    return new Promise<never>(() => {});

});

Last updated

Was this helpful?