feat(web): consume the :PiWeb login handoff before the first route
The daemon hands the browser `http://127.0.0.1:<port>/#t=<token>`. The token is read once, stored through the connection store (so the `pi2-connection` marker is set too and a fresh browser does not bounce to /connect), and stripped from the address bar and history while the query string survives.
This commit is contained in:
1 parent
9ab0830223
commit
51ef1c250b
3 files changed
+184
No files matched your search
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* One-time login handoff for `:PiWeb`.
|
||||
*
|
||||
* The daemon opens `http://<host>:<port>/#t=<token>` (or `#token=<token>`).
|
||||
* The token is read once, stored through the existing device-token helper and
|
||||
* removed from the address bar/history before the router resolves its first
|
||||
* route. Anything that is not a valid handoff is left untouched, so a bad
|
||||
* link stays visibly broken instead of silently landing on `/connect`.
|
||||
*/
|
||||
|
||||
/** Fragment forms the daemon may hand off: `#t=<token>` and `#token=<token>`. */
|
||||
export const HANDOFF_HASH_PATTERN = /^(?:t|token)=(.+)$/;
|
||||
|
||||
/** Device tokens are base64url-ish and at least 20 characters long. */
|
||||
export const HANDOFF_TOKEN_PATTERN = /^[A-Za-z0-9_-]{20,}$/;
|
||||
|
||||
export interface HandoffLocation {
|
||||
readonly hash: string;
|
||||
readonly pathname: string;
|
||||
readonly search: string;
|
||||
}
|
||||
|
||||
export interface HandoffHistory {
|
||||
replaceState(data: unknown, unused: string, url?: string | URL | null): void;
|
||||
}
|
||||
|
||||
export type HandoffTokenSink = (token: string | null) => void;
|
||||
|
||||
/**
|
||||
* Extract a valid handoff token from a location hash. Returns `null` for any
|
||||
* other fragment, including malformed percent-encoding.
|
||||
*/
|
||||
export function readHandoff(hash: string): string | null {
|
||||
if (!hash) return null;
|
||||
const fragment = hash.startsWith("#") ? hash.slice(1) : hash;
|
||||
const match = HANDOFF_HASH_PATTERN.exec(fragment);
|
||||
const encoded = match?.[1];
|
||||
if (!encoded) return null;
|
||||
|
||||
let token: string;
|
||||
try {
|
||||
token = decodeURIComponent(encoded);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
return HANDOFF_TOKEN_PATTERN.test(token) ? token : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a handoff token and strip the fragment while keeping the path and
|
||||
* query string. No-ops (and never writes history) when the hash is not a
|
||||
* valid handoff.
|
||||
*/
|
||||
export function consumeHandoff(
|
||||
location: HandoffLocation,
|
||||
history: HandoffHistory,
|
||||
setToken: HandoffTokenSink,
|
||||
): string | null {
|
||||
const token = readHandoff(location.hash);
|
||||
if (!token) return null;
|
||||
|
||||
setToken(token);
|
||||
history.replaceState(null, "", location.pathname + location.search);
|
||||
return token;
|
||||
}
|
||||
@@ -8,7 +8,9 @@ import "@/styles/app.css";
|
||||
import "@/lib/i18n";
|
||||
|
||||
import { Toaster } from "@/components/ui/toast";
|
||||
import { consumeHandoff } from "@/lib/handoff";
|
||||
import { router } from "@/router";
|
||||
import { useConnectionStore } from "@/stores/connection";
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
@@ -39,6 +41,14 @@ function App() {
|
||||
);
|
||||
}
|
||||
|
||||
// `:PiWeb` hands the device token over in the fragment. Consume it before the
|
||||
// router resolves its first route, and go through the connection store (not
|
||||
// `setDeviceToken`) so the `pi2-connection` marker the guards read is set too:
|
||||
// otherwise a fresh browser would still bounce to `/connect` on its first load.
|
||||
consumeHandoff(window.location, window.history, (token) => {
|
||||
useConnectionStore.getState().connect({ token, endpoint: window.location.origin });
|
||||
});
|
||||
|
||||
const container = document.getElementById("root");
|
||||
if (!container) throw new Error("Missing #root element");
|
||||
createRoot(container).render(<App />);
|
||||
Reference in new issue
Block a user