Critical: Always refresh Graph token before file operations to prevent expiration errors

This commit is contained in:
2026-01-18 04:06:13 +00:00
parent 00b944c388
commit 1322ace853
+47 -7
View File
@@ -2329,7 +2329,14 @@
if (!link || fileCache.has(link)) return; // Skip if no link or already cached
try {
const graphToken = await ensureGraphToken();
// Always refresh token before critical file operation to ensure it's fresh
let graphToken = await refreshGraphToken();
// If no token after refresh, try to ensure one exists
if (!graphToken) {
graphToken = await ensureGraphToken();
}
const headers = graphToken ? { 'x-graph-token': graphToken } : {};
const controller = new AbortController();
@@ -2865,7 +2872,14 @@
</div>
`;
const graphToken = await ensureGraphToken();
// Always refresh token before opening folder to ensure it's fresh
let graphToken = await refreshGraphToken();
// If no token after refresh, try to ensure one exists
if (!graphToken) {
graphToken = await ensureGraphToken();
}
const headers = graphToken ? { 'x-graph-token': graphToken } : {};
try {
@@ -2907,11 +2921,37 @@
console.error('❌ job-files error for', job.Job_Number, err);
const authExpired = err?.message === 'AUTH_EXPIRED' || String(err || '').includes('401');
const timedOut = err?.name === 'AbortError';
const friendly = authExpired
? 'Your Microsoft Graph session expired. Click Re-authenticate to sign in again.'
: timedOut
? 'Request timed out. If this keeps happening, re-authenticate and retry.'
: (err?.message || 'Unknown error');
// If auth expired, silently clear and redirect
if (authExpired) {
try { pb?.authStore?.clear?.(); } catch {}
try { localStorage.removeItem(GRAPH_TOKEN_KEY); } catch {}
try { localStorage.removeItem('pocketbase_auth'); } catch {}
try { sessionStorage.clear(); } catch {}
// Show error but with auto-redirect option
iframeLoader.classList.remove('hidden');
iframeLoader.innerHTML = `
<div class="text-center max-w-md mx-auto px-4 space-y-4">
<div class="text-xl font-semibold text-red-600">Unable to load files</div>
<div class="text-gray-600">Your Microsoft Graph session expired. Click Re-authenticate to sign in again.</div>
<div class="flex items-center justify-center gap-2 flex-wrap">
<a href="${link}" target="_blank" rel="noopener" class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 inline-block font-medium">Open folder instead</a>
<button id="reauthBtn" class="px-4 py-2 bg-gray-300 hover:bg-gray-400 rounded-lg text-gray-800 font-medium">Re-authenticate</button>
</div>
</div>
`;
const reauthBtn = document.getElementById('reauthBtn');
if (reauthBtn) {
reauthBtn.addEventListener('click', () => {
window.location.href = 'signin.html';
});
}
return;
}
const friendly = timedOut
? 'Request timed out. If this keeps happening, re-authenticate and retry.'
: (err?.message || 'Unknown error');
iframeLoader.classList.remove('hidden');
iframeLoader.innerHTML = `
<div class="text-center max-w-md mx-auto px-4 space-y-3">