T14.2 (D27): the offline sign-in window follows the server

/api/auth/me returns session_expires_at (the session token exp, UTC). The browser
keeps it and never extends it: the fixed 12 hours and the sliding reset are gone.
Offline before that time the app opens as normal. Offline after it the app opens in
read-and-queue mode: no redirect, one mode line, changes are queued and held until
a page load gets a good answer from the server. wpCacheAuth refreshes the cached
user without moving the end time.

D26 and D27 recorded as Accepted. tests/session_expiry_check.py (22 checks) fails
against the old 12 hour rule.
This commit is contained in:
2026-10-17 14:18:53 -07:00
parent 73f0b5e69d
commit 40827d5371
7 changed files with 314 additions and 29 deletions

View File

@@ -163,6 +163,31 @@
}
}
// D27. The offline sign-in window is the SERVER session, not a timer of the
// browser's own. /api/auth/me returns `session_expires_at` (the session token's
// exp), and that is what the cache keeps. Only a good answer from the server
// changes it: loading a page offline, or saving preferences, never extends it.
// (This replaced a fixed 12 hour window that slid forward on every page load and
// disagreed with AUTH_SESSION_HOURS.)
function readAuthCache() {
try { return JSON.parse(localStorage.getItem('wp_auth_cache') || 'null'); } catch (e) { return null; }
}
function writeAuthCache(user, expMs) {
try { if (user) localStorage.setItem('wp_auth_cache', JSON.stringify({ user: user, exp: expMs || 0, at: Date.now() })); } catch (e) {}
}
// For callers that refresh the cached user (the preferences dialog): the user
// changes, the end of the session does not.
window.wpCacheAuth = function (user) {
var c = readAuthCache();
writeAuthCache(user, c && c.exp);
};
// 'online' (a good answer from the server), 'offline' (opened from the cache
// before the session ended) or 'readqueue' (opened from the cache after it ended:
// cached data can be read and changes are queued, but nothing is sent until a
// good sign-in). The sync badge shows the one line for 'readqueue'.
window.WP_SESSION_MODE = 'online';
fetch('/api/auth/me', { headers: { 'Accept': 'application/json' } })
.then(function (r) {
if (r.status === 401 || r.status === 403) { try { localStorage.removeItem('wp_auth_cache'); } catch (e) {} goToLogin(); return; }
@@ -171,17 +196,26 @@
var user = data && data.user;
// Remember the last good auth so the PWA can open offline. The server is
// still the real gate; offline writes queue in the outbox until reconnect.
try { if (user) localStorage.setItem('wp_auth_cache', JSON.stringify({ user: user, at: Date.now() })); } catch (e) {}
var expMs = data && data.session_expires_at ? Date.parse(data.session_expires_at) : 0;
writeAuthCache(user, isNaN(expMs) ? 0 : expMs);
proceed(user);
});
})
.catch(function () {
// Offline / API unreachable: fall back to a recent cached auth if present,
// so the app (and the field view) still open without a network.
try {
var c = JSON.parse(localStorage.getItem('wp_auth_cache') || 'null');
if (c && c.user && (Date.now() - (c.at || 0)) < 12 * 3600 * 1000) { proceed(c.user); return; }
} catch (e) {}
// Offline / API unreachable: open from the cached sign-in so the app (and the
// field view) still work without a network. With no cache this device has
// never signed in, so there is nothing to open: go to the sign-in page.
var c = readAuthCache();
if (c && c.user) {
// Before the session ends: open as normal. After it (or when the cache has
// no end time, an older cache): read and queue. The page does NOT redirect,
// because login.html cannot reach Okta without a network.
var live = !!c.exp && Date.now() < c.exp;
window.WP_SESSION_MODE = live ? 'offline' : 'readqueue';
proceed(c.user);
try { document.dispatchEvent(new CustomEvent('wp-session-mode', { detail: window.WP_SESSION_MODE })); } catch (e) {}
return;
}
goToLogin();
});
})();