Handle redirect on login page and clear stuck MSAL state
This commit is contained in:
parent
60b9329da2
commit
f5aa512629
1 changed files with 45 additions and 5 deletions
|
|
@ -295,16 +295,34 @@
|
|||
};
|
||||
|
||||
let msalInstance = null;
|
||||
let msalReady = false;
|
||||
|
||||
// Initialize MSAL
|
||||
// Initialize MSAL and handle any pending redirects
|
||||
async function initMsal() {
|
||||
msalInstance = new msal.PublicClientApplication(msalConfig);
|
||||
await msalInstance.initialize();
|
||||
try {
|
||||
msalInstance = new msal.PublicClientApplication(msalConfig);
|
||||
await msalInstance.initialize();
|
||||
|
||||
// Handle any pending redirect (in case user lands on login page after OAuth)
|
||||
const response = await msalInstance.handleRedirectPromise();
|
||||
if (response && response.accessToken) {
|
||||
// User completed auth, send token to server
|
||||
await sendTokenToServer(response.accessToken);
|
||||
return;
|
||||
}
|
||||
|
||||
msalReady = true;
|
||||
} catch (error) {
|
||||
console.error("MSAL init error:", error);
|
||||
// Clear any stuck interaction state
|
||||
sessionStorage.clear();
|
||||
msalReady = true;
|
||||
}
|
||||
}
|
||||
initMsal();
|
||||
|
||||
async function loginWithMicrosoft() {
|
||||
if (!msalInstance) {
|
||||
if (!msalReady) {
|
||||
alert("Please wait, authentication is initializing...");
|
||||
return;
|
||||
}
|
||||
|
|
@ -315,7 +333,29 @@
|
|||
await msalInstance.loginRedirect(loginRequest);
|
||||
} catch (error) {
|
||||
console.error("Login error:", error);
|
||||
alert("Login failed: " + error.message);
|
||||
if (error.errorCode === "interaction_in_progress") {
|
||||
// Clear stuck state and retry
|
||||
sessionStorage.clear();
|
||||
alert("Please click the button again.");
|
||||
} else {
|
||||
alert("Login failed: " + error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function sendTokenToServer(accessToken) {
|
||||
try {
|
||||
const response = await fetch("/solventum-image-metadata/auth/token", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ access_token: accessToken })
|
||||
});
|
||||
const data = await response.json();
|
||||
if (data.success && data.redirect) {
|
||||
window.location.href = data.redirect;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Server error:", error);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue