v3: the frontend stops trusting what it last saw — a review of src/ turned up counts that fell to zero, stale copies winning over fresh ones, sessions that tabs trampled on each other, and a player queue that outlived its listener, and this closes them. Voting or liking something with no live count yet started from 0, so a dump with five votes read 1 on the click and, with the socket down, 0 for good once the undo fired; the buttons now pass the count they show, which the optimistic update and its undo start from. After a reconnect the chat re-read its history as the truth, but the merge let the live copy of a message win, so an edit made while the socket was down never showed; the resync now replaces live copies with what it fetched. Every dump_updated added the dump to the live list if it wasn't there, so retitling a months-old dump put it at the top of New, Hot and Journal for everyone, while one that had arrived live kept its old title; an edit now refreshes a live entry in place and adds nothing, and the server announces a dump going public as a dump_created, which is the one case that is an arrival. The new-dump modal let a slow preview for the previous link land on the new one, title suggestion included; changing the link now aborts both lookups. Markdown found mentions by rewriting the source text with a rule that, unlike the server's, didn't skip an @ after a word character, so an email address linked its domain, a URL like mastodon.social/@bob was split, and @ in code came out as literal link syntax; mentions are now found by a remark step over text nodes only, by the server's rule. Changing the password in one tab stored fresh tokens that any other open tab wiped the moment its old token drew a 401; the session now lives behind utils/authStorage.ts, tabs follow each other through storage events (so a logout also signs out every tab), a 401 only forgets the stored session if it still holds that token, and in-place session updates go through a new updateSession() rather than re-reading localStorage. The persisted player queue carries media-token URLs yet survived logout for the next person to sign in; it is now stamped with its owner, not restored for anyone else, cleared on logout or account switch (a guest's queue survives signing in), and older unstamped queues holding a file are dropped. Playlist checkboxes in the new-dump and add-to-playlist modals flipped whether or not the server agreed and now wait for it. CommentThread and the Dump page's loads use authFetch like the rest, so a 401 there signs out consistently; isTokenExpired has one home; and the useScrollLock imports carry their extension so deno check passes.
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 42s
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 42s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
59
src/utils/authStorage.ts
Normal file
59
src/utils/authStorage.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { type AuthResponse, deserializeAuthResponse } from "../model.ts";
|
||||
|
||||
// The session is shared by every tab through this one localStorage entry.
|
||||
export const AUTH_STORAGE_KEY = "authResponse";
|
||||
|
||||
export function isTokenExpired(token: string): boolean {
|
||||
try {
|
||||
const payload = JSON.parse(atob(token.split(".")[1]));
|
||||
return typeof payload.exp === "number" && payload.exp * 1000 < Date.now();
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/** The stored session, or null — discarding one that can no longer be used. */
|
||||
export function readStoredSession(): AuthResponse | null {
|
||||
const stored = localStorage.getItem(AUTH_STORAGE_KEY);
|
||||
if (!stored) return null;
|
||||
|
||||
let parsed;
|
||||
try {
|
||||
parsed = deserializeAuthResponse(JSON.parse(stored));
|
||||
} catch {
|
||||
localStorage.removeItem(AUTH_STORAGE_KEY);
|
||||
return null;
|
||||
}
|
||||
if (isTokenExpired(parsed.token)) {
|
||||
localStorage.removeItem(AUTH_STORAGE_KEY);
|
||||
return null;
|
||||
}
|
||||
// Sessions persisted before roles existed have no `role`; the server also
|
||||
// rejects their tokens, but discard them here so the UI never renders with
|
||||
// a roleless user.
|
||||
if (!parsed.user.role) {
|
||||
localStorage.removeItem(AUTH_STORAGE_KEY);
|
||||
return null;
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
export function storeSession(session: AuthResponse): void {
|
||||
localStorage.setItem(AUTH_STORAGE_KEY, JSON.stringify(session));
|
||||
}
|
||||
|
||||
/**
|
||||
* Forget the stored session, but only if it is still the one holding `token`.
|
||||
* Another tab may already have replaced it — a password change there hands out
|
||||
* fresh tokens — and a 401 for the old token here must not wipe those.
|
||||
*/
|
||||
export function forgetStoredSession(token: string | undefined): void {
|
||||
const stored = localStorage.getItem(AUTH_STORAGE_KEY);
|
||||
if (!stored) return;
|
||||
try {
|
||||
if (JSON.parse(stored)?.token !== token) return;
|
||||
} catch {
|
||||
// Unreadable: nobody can use it.
|
||||
}
|
||||
localStorage.removeItem(AUTH_STORAGE_KEY);
|
||||
}
|
||||
Reference in New Issue
Block a user