app-history.js was redefining toggleDarkMode() and loadTheme() with a broken implementation (body.classList + wrong localStorage key 'darkMode') that overrode the correct versions in utils.js. Removed duplicates so utils.js handles both pages consistently via data-theme on :root. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
96 lines
3.1 KiB
JavaScript
96 lines
3.1 KiB
JavaScript
/* MSAL auth + init for history.html */
|
|
|
|
const msalConfig = {
|
|
auth: {
|
|
clientId: '',
|
|
authority: '',
|
|
redirectUri: window.location.origin + window.location.pathname
|
|
},
|
|
cache: { cacheLocation: 'localStorage', storeAuthStateInCookie: false }
|
|
};
|
|
|
|
let msalInstance = null;
|
|
window.msalToken = null;
|
|
|
|
function initMsal() {
|
|
const el = document.getElementById('msalConfig');
|
|
if (!el) return;
|
|
const tenantId = el.dataset.tenantId;
|
|
const clientId = el.dataset.clientId;
|
|
const redirectUri = el.dataset.redirectUri;
|
|
if (!tenantId || !clientId) return;
|
|
|
|
msalConfig.auth.clientId = clientId;
|
|
msalConfig.auth.authority = `https://login.microsoftonline.com/${tenantId}`;
|
|
if (redirectUri) msalConfig.auth.redirectUri = redirectUri;
|
|
|
|
const script = document.createElement('script');
|
|
script.src = 'https://cdn.jsdelivr.net/npm/@azure/msal-browser@2/lib/msal-browser.min.js';
|
|
script.onload = () => {
|
|
msalInstance = new msal.PublicClientApplication(msalConfig);
|
|
msalInstance.initialize().then(handleMsalRedirect);
|
|
};
|
|
document.head.appendChild(script);
|
|
}
|
|
|
|
async function handleMsalRedirect() {
|
|
try {
|
|
const response = await msalInstance.handleRedirectPromise();
|
|
if (response) {
|
|
window.msalToken = response.accessToken;
|
|
showAuthenticatedUI(response.account);
|
|
return;
|
|
}
|
|
} catch (e) { console.error('MSAL redirect error:', e); }
|
|
|
|
const accounts = msalInstance.getAllAccounts();
|
|
if (accounts.length > 0) {
|
|
try {
|
|
const tokenResponse = await msalInstance.acquireTokenSilent({ scopes: ['User.Read'], account: accounts[0] });
|
|
window.msalToken = tokenResponse.accessToken;
|
|
showAuthenticatedUI(accounts[0]);
|
|
} catch (e) { showLoginUI(); }
|
|
} else {
|
|
if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
|
|
showAuthenticatedUI(null);
|
|
} else {
|
|
showLoginUI();
|
|
}
|
|
}
|
|
}
|
|
|
|
function showLoginUI() {
|
|
const overlay = document.getElementById('authOverlay');
|
|
if (overlay) overlay.classList.add('active');
|
|
}
|
|
|
|
function showAuthenticatedUI(account) {
|
|
const overlay = document.getElementById('authOverlay');
|
|
if (overlay) overlay.classList.remove('active');
|
|
|
|
const userInfo = document.getElementById('userInfo');
|
|
if (userInfo && account) userInfo.textContent = account.name || account.username;
|
|
|
|
const logoutBtn = document.getElementById('logoutBtn');
|
|
if (logoutBtn) logoutBtn.style.display = 'inline-block';
|
|
|
|
const historySection = document.getElementById('historySection');
|
|
if (historySection) historySection.style.display = '';
|
|
|
|
loadHistory();
|
|
}
|
|
|
|
async function loginWithMicrosoft() {
|
|
if (!msalInstance) return;
|
|
try { await msalInstance.loginRedirect({ scopes: ['User.Read'] }); }
|
|
catch (e) { console.error('Login failed:', e); alert('Login failed. Please try again.'); }
|
|
}
|
|
|
|
function logout() {
|
|
if (msalInstance) msalInstance.logoutRedirect();
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
loadTheme(); // from utils.js — sets data-theme on :root
|
|
initMsal();
|
|
});
|