diff --git a/templates/login.html b/templates/login.html
index a4f8ea1..9cc37df 100644
--- a/templates/login.html
+++ b/templates/login.html
@@ -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);
}
}