fix(native): the app's browser sign-in no longer lands on the dashboard when the browser still holds an older TREK

A browser that ran TREK before 5.1.1 keeps that version's app in its service
worker. When the app opened /native-handoff, the worker answered it from its
cache with an app that has no such page, which sent the user to /, and the
sign-in ended on the dashboard in the browser instead of in the app.

The app now opens /api/auth/native/start, which every TREK service worker
passes to the server. That page takes the workers off the browser and
forwards to the handoff page. Until the sign-in is handed back, the web app
registers no service worker and does not reload for a new version: a tab
still open on the older TREK keeps its worker alive, and a registration or a
reload in the middle of the sign-in brought it back to answer the next page.
This commit is contained in:
Maurice
2026-11-09 22:21:09 +01:00
parent 5420077b98
commit 034cfb539a
14 changed files with 286 additions and 7 deletions
+6 -4
View File
@@ -44,10 +44,12 @@ describe('requestHandoffCode', () => {
});
describe('handoffUrl', () => {
it('points at the hand-off page with the challenge and an optional start', () => {
expect(handoffUrl('https://trek.example.com', 'c')).toBe('https://trek.example.com/native-handoff?challenge=c');
it('starts at the server page in front of the hand-off page, with the challenge and an optional start', () => {
// Under /api, which every TREK service worker passes to the server: an older
// version's worker must not answer the hand-off page from its cache.
expect(handoffUrl('https://trek.example.com', 'c')).toBe('https://trek.example.com/api/auth/native/start?challenge=c');
expect(handoffUrl('https://trek.example.com', 'c', 'oidc')).toBe(
'https://trek.example.com/native-handoff?challenge=c&start=oidc'
'https://trek.example.com/api/auth/native/start?challenge=c&start=oidc'
);
});
});
@@ -59,7 +61,7 @@ describe('signInThroughBrowser', () => {
await signInThroughBrowser('oidc');
const opened = new URL(vi.mocked(TrekShell.authenticate).mock.calls[0][0].url);
expect(opened.pathname).toBe('/native-handoff');
expect(opened.pathname).toBe('/api/auth/native/start');
expect(opened.searchParams.get('start')).toBe('oidc');
expect(vi.mocked(TrekShell.authenticate).mock.calls[0][0].callbackScheme).toBe('com.liketrek.trek');
const { code, verifier } = vi.mocked(nativeAuthApi.exchange).mock.calls[0][0];
+8 -1
View File
@@ -21,6 +21,13 @@ import { APP_URL_SCHEME, TrekShell } from './trekShell';
export const HANDOFF_PATH = '/native-handoff';
/**
* Where the browser is sent first: a page of the server's that takes an older
* TREK's service worker off the browser and then opens HANDOFF_PATH with the
* same query. Under /api, which every TREK service worker passes to the server.
*/
export const HANDOFF_START_PATH = '/api/auth/native/start';
/** Where the browser starts: straight into SSO, or straight at the passkey prompt. */
export type BrowserSignInStart = 'oidc' | 'passkey';
@@ -65,7 +72,7 @@ export function createHandoffSecret(): { verifier: string; challenge: string } {
}
export function handoffUrl(origin: string, challenge: string, start?: BrowserSignInStart, invite?: string): string {
const url = new URL(HANDOFF_PATH, origin);
const url = new URL(HANDOFF_START_PATH, origin);
url.searchParams.set('challenge', challenge);
if (start) url.searchParams.set('start', start);
if (invite) url.searchParams.set('invite', invite);
@@ -0,0 +1,29 @@
import { afterEach, describe, expect, it } from 'vitest';
import { BROWSER_SIGN_IN_KEY, BROWSER_SIGN_IN_TTL_MS, browserSignInRunning, endBrowserSignIn } from './browserSignInFlag';
afterEach(() => localStorage.clear());
describe('browserSignInRunning', () => {
it('FE-NATIVE-SIGNINFLAG-001: counts a mark from public/native-handoff.js for as long as a sign-in may take', () => {
const now = 1_000_000_000;
expect(browserSignInRunning(now)).toBe(false);
localStorage.setItem(BROWSER_SIGN_IN_KEY, String(now - 1000));
expect(browserSignInRunning(now)).toBe(true);
localStorage.setItem(BROWSER_SIGN_IN_KEY, String(now - BROWSER_SIGN_IN_TTL_MS));
expect(browserSignInRunning(now)).toBe(false);
// A clock set back, or anything that is no time at all.
localStorage.setItem(BROWSER_SIGN_IN_KEY, String(now + 60_000));
expect(browserSignInRunning(now)).toBe(false);
localStorage.setItem(BROWSER_SIGN_IN_KEY, 'soon');
expect(browserSignInRunning(now)).toBe(false);
});
it('FE-NATIVE-SIGNINFLAG-002: ends with the hand-back to the app', () => {
localStorage.setItem(BROWSER_SIGN_IN_KEY, String(Date.now()));
endBrowserSignIn();
expect(localStorage.getItem(BROWSER_SIGN_IN_KEY)).toBeNull();
expect(browserSignInRunning()).toBe(false);
});
});
+31
View File
@@ -0,0 +1,31 @@
/**
* Marks the app's sign-in in the system browser while it runs. The page in front
* of the handoff page (public/native-handoff.js) sets it, so that until the user
* is handed back to the app no service worker is registered (public/registerSW.js)
* and no new version reloads the page (utils/versionHandover.ts). A browser tab
* still open on an older TREK keeps that version's worker alive, and a worker
* registered or a reload in the middle of the sign-in would bring it back to
* answer the next page with an app that has no handoff page.
*/
export const BROWSER_SIGN_IN_KEY = 'trek_native_signin';
/** As long as a sign-in in the browser may take; the mark counts for no longer. */
export const BROWSER_SIGN_IN_TTL_MS = 15 * 60 * 1000;
export function browserSignInRunning(now = Date.now()): boolean {
try {
const started = Number(localStorage.getItem(BROWSER_SIGN_IN_KEY));
return Number.isFinite(started) && started > 0 && now - started >= 0 && now - started < BROWSER_SIGN_IN_TTL_MS;
} catch {
return false;
}
}
/** The sign-in is handed back to the app: the browser goes back to normal. */
export function endBrowserSignIn(): void {
try {
localStorage.removeItem(BROWSER_SIGN_IN_KEY);
} catch {
/* site data blocked: the mark runs out by itself */
}
}