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:
@@ -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();
|
||||
});
|
||||
})();
|
||||
|
||||
@@ -276,6 +276,7 @@
|
||||
_mem = _mem.filter(function (o) { return o.opId !== opId; });
|
||||
qWrite(qRead().filter(function (o) { return o.opId !== opId; }));
|
||||
}
|
||||
function readQueueMode() { try { return window.WP_SESSION_MODE === 'readqueue'; } catch (e) { return false; } }
|
||||
function sameUser(a, b) { return String(a || '').toLowerCase() === String(b || '').toLowerCase(); }
|
||||
|
||||
// Append an op, coalescing by (kind,key) so only the latest write per entity is
|
||||
@@ -347,7 +348,9 @@
|
||||
// (429/5xx/network) stay queued.
|
||||
function flush() {
|
||||
if (_flushing) return Promise.resolve();
|
||||
if (_held) { notifySync(); return Promise.resolve(); }
|
||||
// D27: opened offline after the session ended (read and queue). Changes are kept
|
||||
// and wait; nothing is sent until a page load gets a good answer from the server.
|
||||
if (_held || readQueueMode()) { notifySync(); return Promise.resolve(); }
|
||||
var q = sendableOps();
|
||||
if (!q.length) { notifySync(); return Promise.resolve(); }
|
||||
_flushing = true; notifySync();
|
||||
@@ -468,6 +471,11 @@
|
||||
if (c.deviceFull) {
|
||||
el.textContent = '✕ ' + c.unsaved + ' change' + (c.unsaved === 1 ? ' is' : 's are') + ' not saved on this device. Keep this page open until it is sent to the project.';
|
||||
el.style.color = 'var(--wp-status-error-text)'; el.style.borderColor = 'var(--wp-status-error-border-a)'; el.style.background = 'var(--wp-status-error-bg)'; el.style.display = 'inline-flex';
|
||||
} else if (readQueueMode()) {
|
||||
// D27. The one line for read-and-queue mode. T14.3 owns the final wording
|
||||
// and the sign-in control.
|
||||
el.textContent = 'Working offline. Sign in again when you have a signal to send your changes.';
|
||||
el.style.color = 'var(--wp-status-warning-text)'; el.style.borderColor = 'var(--cds-support-warning)'; el.style.background = 'var(--wp-status-warning-bg)'; el.style.display = 'inline-flex';
|
||||
} else if (c.held) {
|
||||
el.textContent = '⚠ Sign in again to send your changes to the project. They are kept on this device.';
|
||||
el.style.color = 'var(--wp-status-warning-text)'; el.style.borderColor = 'var(--cds-support-warning)'; el.style.background = 'var(--wp-status-warning-bg)'; el.style.display = 'inline-flex';
|
||||
@@ -501,6 +509,7 @@
|
||||
// D26: the one thing that releases a held queue. auth-guard fires this after a
|
||||
// good /api/auth/me (or the offline cache), and the next flush finds out for
|
||||
// itself whether the session is really good: a 401 holds the queue again.
|
||||
document.addEventListener('wp-session-mode', function () { notifySync(); });
|
||||
document.addEventListener('wp-auth-ready', function () { _held = false; _backoff = 0; notifySync(); scheduleFlush(0); });
|
||||
} catch (e) {}
|
||||
if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { notifySync(); scheduleFlush(0); }); }
|
||||
|
||||
@@ -220,7 +220,8 @@
|
||||
.then(function (res) {
|
||||
if (!res.ok) { msg((res.j && res.j.detail) || ('Could not save (HTTP ' + res.status + ').'), false); return; }
|
||||
if (res.j && res.j.user) window.WP_USER = res.j.user;
|
||||
try { localStorage.setItem('wp_auth_cache', JSON.stringify({ user: window.WP_USER, at: Date.now() })); } catch (e) {}
|
||||
// D27: refresh the cached user, but not the end of the session.
|
||||
try { if (window.wpCacheAuth) window.wpCacheAuth(window.WP_USER); } catch (e) {}
|
||||
msg('Saved. Reloading so every date on the page agrees…', true);
|
||||
// Dates are formatted at render time all over the app; a reload is the
|
||||
// honest way to apply the change everywhere at once.
|
||||
|
||||
Reference in New Issue
Block a user