👨🔧Usage
Let's get your App authenticated!
// Import the necessary functions from oidc-spa library
import { createOidc, decodeJwt } from "oidc-spa";
// Initialize OIDC client with configuration options
const oidc = await createOidc({
issuerUri: "https://auth.your-domain.net/auth/realms/myrealm",
clientId: "myclient",
/**
* Optional, you can modify the url before redirection to the identity
* server. Alternatively you can use:
* getExtraQueryParams: ()=> ({ ui_locales: "fr" })
*/
transformUrlBeforeRedirect: url => `${url}&ui_locales=fr`
/**
* This parameter have to be provided if your App is not hosted at the
* origin of the domain.
* For example if your site is accessed by navigating to
* https://www.example.com you don't have to provide this parameter.
* On the other end if your site is accessed by navigating to
* https://www.example.com/my-app
* Then you want to set publicUrl to `/my-app`
* (or `https://www.example.com/my-app`).
* If you are using Vite: `publicUrl: import.meta.env.BASE_URL`
* If you using Create React App: `publicUrl: process.env.PUBLIC_URL`
*
* Be mindful that `${window.location.origin}${publicUrl}/silent-sso.html`
* must return the `silent-sso.html` that you have created in the previous
* step.
*/
//publicUrl: import.meta.env.BASE_URL
});
if (!oidc.isUserLoggedIn) {
// This return a promise that never resolve.
// The user will be redirected to the identity server.
oidc.login({
/**
* doesCurrentHrefRequiresAuth determines the behavior when a user
* gives up on loggin in and navigate back.
* When it happens we don't want to send him back to a page that
* can't be accessed without authentication.
*
* If you are calling login() as a result of the user clicking
* on a 'login' button (like here) you should set
* doesCurrentHrefRequiresAuth to false.
*
* When you are calling login because your user navigated to a path
* that requires authentication you should set
* doesCurrentHrefRequiresAuth to true
*/
doesCurrentHrefRequiresAuth: false
/** Optionally, you can add some extra parameter
* to be added on the login url.
*/
//extraQueryParams: { kc_idp_hint: "google" }
});
} else {
const {
// The accessToken is what you'll use as a Bearer token to
// authenticate to your APIs
accessToken,
// You can parse the idToken as a JWT to get some information
// about the user.
idToken
} = oidc.getTokens();
// NOTE: In most application you do not need to look into the JWT of the
// idToken on the frontend, you usually obtain the user info by querying
// a GET /user endpoint with a authorization header like
// `Bearer <accessToken>`.
const user = decodeJwt(idToken) as {
// Use https://jwt.io/ to tell what's in your idToken
sub: string;
preferred_username: string;
};
console.log(`Hello ${user.preferred_username}`);
// To call when the user click on logout.
// You can also redirect to a custom url with
// { redirectTo: "specific url", url: `${location.origin}/bye` }
oidc.logout({ redirectTo: "home" });
}Refreshing token
What happens if the OIDC server is down?
Last updated
Was this helpful?