I’m using Keycloak in a React app and trying to implement reauthentication when a user clicks a button. The idea is to reauthenticate without redirecting the user to the Keycloak login page if they’re already logged in via SSO. Here’s my current function:
const handleReauthClick = useCallback(() => {
if (!keycloak) {
console.error("Keycloak is not initialized.");
return;
}
const idpHint = storedTokenData?.identity_provider; // getting the identity_provider from localstorage
console.log("Starting silent reauthentication...");
keycloak
.login({
redirectUri: window.location.href,
prompt: "none",
idpHint,
})
.then(() => {
if (keycloak.authenticated) {
console.log("Silent reauthentication successful.");
} else {
console.warn("Silent reauthentication failed; redirecting to Keycloak login.");
redirectToKeycloak(idpHint);
}
})
.catch((error) => {
console.error("SSO reauthentication error:", error);
})
.finally(() => {
console.log("Reauthentication attempt completed.");
doSomethingButton("needToTriggerSomething");
});
}, [keycloak, redirectToKeycloak, doSomethingButton]);
The doSomethingButton function should only run if reauthentication is successful. However, I’m facing these issues:
- No Logs on Silent Reauth: I don’t see logs like Silent reauthentication successful. even though I see an authentication event in Keycloak. It seems like keycloak.login({ prompt: “none” }) isn’t resolving as expected.
- Page Reloads without doSomethingButton: The page reloads without triggering doSomethingButton.
At this point im not even sure if im actually really re-authenticating properly. Any insights will be wonderful