Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(app): keep people signed in when moving to fluxer.com

Hampus28a19e0fcd39
diff --git a/fluxer_app/src/features/app/domain_migration/DomainMigrationAppStorage.ts b/fluxer_app/src/features/app/domain_migration/DomainMigrationAppStorage.ts
index c7295bb98..942788557 100644
--- a/fluxer_app/src/features/app/domain_migration/DomainMigrationAppStorage.ts
+++ b/fluxer_app/src/features/app/domain_migration/DomainMigrationAppStorage.ts
@@ -3,6 +3,7 @@
import {
type DomainMigrationAppStorageEntry,
isExportableLocalStorageKey,
+ type StorageLike,
} from '@app/features/app/domain_migration/DomainMigrationCore';
import type {RuntimeConfigSnapshot} from '@app/features/app/state/InstanceSnapshotStore';
import type {StoredAccount} from '@app/features/auth/state/AccountStorageContract';
@@ -13,7 +14,7 @@ import {
LEGACY_APP_STORAGE_MIGRATION_MARKER_KEY,
LEGACY_APP_STORAGE_UNCLASSIFIED_KEYS_KEY,
} from '@app/features/platform/state/LegacyAppStorageMigration';
-import {GLOBAL_APP_STORAGE_SCOPE} from '@app/features/platform/state/PersistentStorage';
+import {GLOBAL_APP_STORAGE_SCOPE, RAW_MIRROR_SCOPE_PREFIX} from '@app/features/platform/state/PersistentStorage';
import type {AppStorageWrite, PersistentStorageBackend} from '@app/features/platform/state/PersistentStorageBackend';
export interface DomainMigrationAppStorageExport {
@@ -26,7 +27,7 @@ const DENIED_APP_STORAGE_KEYS: ReadonlySet<string> = new Set([
LEGACY_APP_STORAGE_UNCLASSIFIED_KEYS_KEY,
]);
-export function isExportableAppStorageKey(key: string): boolean {
+function isExportableAppStorageKey(key: string): boolean {
return !DENIED_APP_STORAGE_KEYS.has(key) && isExportableLocalStorageKey(key);
}
@@ -133,3 +134,82 @@ export async function writeImportedAppStorage(
await backend.setMany(writes);
}
}
+
+export function rewriteImportedAccount(record: StoredAccount, instance: RuntimeConfigSnapshot): StoredAccount {
+ const managed = Object.fromEntries(
+ Object.entries(record.managedStorageData ?? record.localStorageData ?? {}).filter(([key]) =>
+ isExportableLocalStorageKey(key),
+ ),
+ );
+ return {
+ ...record,
+ localStorageData: managed,
+ managedStorageData: managed,
+ instance,
+ storageKey: accountStorageKey(record.userId, instance) ?? undefined,
+ };
+}
+
+export function importedAccountKeyMoves(
+ accounts: ReadonlyArray<StoredAccount>,
+ instance: RuntimeConfigSnapshot,
+): Map<string, string> {
+ const moves = new Map<string, string>();
+ for (const account of accounts) {
+ const from = deriveAccountStorageKey(account);
+ const to = accountStorageKey(account.userId, instance);
+ if (from !== null && to !== null && from !== to) {
+ moves.set(from, to);
+ }
+ }
+ return moves;
+}
+
+function movedRawMirrorName(name: string, moves: ReadonlyMap<string, string>): string | null {
+ for (const [from, to] of moves) {
+ const prefix = `${RAW_MIRROR_SCOPE_PREFIX}${from}::`;
+ if (name.startsWith(prefix)) {
+ return `${RAW_MIRROR_SCOPE_PREFIX}${to}::${name.slice(prefix.length)}`;
+ }
+ }
+ return null;
+}
+
+export async function retargetAccountKeys(
+ backend: PersistentStorageBackend,
+ storage: StorageLike | null,
+ moves: ReadonlyMap<string, string>,
+): Promise<void> {
+ if (moves.size === 0) {
+ return;
+ }
+ if (storage) {
+ const pointer = moves.get(storage.getItem(AppStorageKey.AUTH_ACCOUNT_KEY) ?? '');
+ if (pointer !== undefined) {
+ storage.setItem(AppStorageKey.AUTH_ACCOUNT_KEY, pointer);
+ }
+ const names: Array<string> = [];
+ for (let index = 0; index < storage.length; index++) {
+ const name = storage.key(index);
+ if (name !== null) {
+ names.push(name);
+ }
+ }
+ for (const name of names) {
+ const moved = movedRawMirrorName(name, moves);
+ const value = moved === null ? null : storage.getItem(name);
+ if (moved === null || value === null) {
+ continue;
+ }
+ try {
+ storage.setItem(moved, value);
+ storage.removeItem(name);
+ } catch {}
+ }
+ }
+ const global = await backend.load(GLOBAL_APP_STORAGE_SCOPE);
+ const pointer = moves.get(global.get(AppStorageKey.AUTH_ACCOUNT_KEY)?.value ?? '');
+ if (pointer !== undefined) {
+ await backend.setMany([{scope: GLOBAL_APP_STORAGE_SCOPE, key: AppStorageKey.AUTH_ACCOUNT_KEY, value: pointer}]);
+ }
+}
diff --git a/fluxer_app/src/features/app/domain_migration/DomainMigrationBrowser.ts b/fluxer_app/src/features/app/domain_migration/DomainMigrationBrowser.ts
index 9e785c7a4..d47698227 100644
--- a/fluxer_app/src/features/app/domain_migration/DomainMigrationBrowser.ts
+++ b/fluxer_app/src/features/app/domain_migration/DomainMigrationBrowser.ts
@@ -49,13 +49,13 @@ function domainMigrationDiscoveryApiEndpoint(): string | null {
}
}
-export async function loadDomainMigrationDiscovery(): Promise<void> {
+export async function loadDomainMigrationDiscovery(timeoutMs = DOMAIN_MIGRATION_DISCOVERY_TIMEOUT_MS): Promise<void> {
if (domainMigrationDiscoveryApiEndpoint() === null) {
return;
}
await InstanceSnapshotStore.resolve({
input: window.location.origin,
- signal: AbortSignal.timeout(DOMAIN_MIGRATION_DISCOVERY_TIMEOUT_MS),
+ signal: AbortSignal.timeout(timeoutMs),
});
}
diff --git a/fluxer_app/src/features/app/domain_migration/DomainMigrationCore.ts b/fluxer_app/src/features/app/domain_migration/DomainMigrationCore.ts
index 67dd690bd..067b31b1a 100644
--- a/fluxer_app/src/features/app/domain_migration/DomainMigrationCore.ts
+++ b/fluxer_app/src/features/app/domain_migration/DomainMigrationCore.ts
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
-import type {RuntimeConfigSnapshot} from '@app/features/app/state/RuntimeConfig';
import type {StoredAccount} from '@app/features/auth/state/AccountStorageContract';
import {isIOSMobileOrTabletUserAgent} from '@app/features/platform/notifications/NotificationAlertOptions';
import {isRecord} from '@fluxer/record_utils/src/RecordGuards';
@@ -19,7 +18,7 @@ const DOMAIN_MIGRATION_TARGET_TO_SOURCE: Readonly<Record<string, string>> = Obje
export const DOMAIN_MIGRATION_MARKER_KEY = 'fluxer:domain-migration';
export const DOMAIN_MIGRATION_DEVICE_KEY = 'fluxer:domain-migration:device';
export const DOMAIN_MIGRATION_PENDING_KEY = 'fluxer:domain-migration:pending';
-export const DOMAIN_MIGRATION_INTENT_KEY = 'fluxer:domain-migration:intent';
+const DOMAIN_MIGRATION_INTENT_KEY = 'fluxer:domain-migration:intent';
export const DOMAIN_MIGRATION_NOTIFICATIONS_KEY = 'fluxer:domain-migration:notifications';
export const DOMAIN_MIGRATION_MOVED_DISMISSED_KEY = 'fluxer:domain-migration:moved-dismissed-at';
const DOMAIN_MIGRATION_ENROLLED_KEY = 'fluxer:domain-migration:enrolled';
@@ -35,7 +34,7 @@ const DOMAIN_MIGRATION_FAILED_RETRY_DELAY_MS = 24 * 60 * 60 * 1000;
export const DOMAIN_MIGRATION_PENDING_MAX_AGE_MS = 10 * 60 * 1000;
export const DOMAIN_MIGRATION_CUSTOM_SOUNDS_MAX_BYTES = 4 * 1024 * 1024;
export const DOMAIN_MIGRATION_THEME_ASSETS_MAX_BYTES = 2 * 1024 * 1024;
-export const DOMAIN_MIGRATION_MOVED_DISMISS_MS = 7 * 24 * 60 * 60 * 1000;
+const DOMAIN_MIGRATION_MOVED_DISMISS_MS = 7 * 24 * 60 * 60 * 1000;
const DOMAIN_MIGRATION_PROBE_INTERVAL_MS = 6 * 60 * 60 * 1000;
const DOMAIN_MIGRATION_IMPORT_STALE_MS = 2 * 60 * 1000;
const DOMAIN_MIGRATION_DEVICES_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;
@@ -48,7 +47,6 @@ const ONE_SHOT_ROUTE_PREFIXES: ReadonlyArray<string> = [
'/oauth2/authorize',
'/auth/sso/callback',
'/premium-callback',
- '/age-verification-callback',
'/connection-callback',
];
@@ -112,7 +110,7 @@ export function sanitizeNextPath(value: unknown): string {
return `${pathname}${url.search}${url.hash}`;
}
-export function isDomainMigrationStorageKey(key: string): boolean {
+function isDomainMigrationStorageKey(key: string): boolean {
return key.startsWith(DOMAIN_MIGRATION_MARKER_KEY);
}
@@ -345,9 +343,10 @@ export function writeDomainMigrationDeviceMap(storage: StorageLike | null, map:
} catch {}
}
-export interface DomainMigrationIntent {
+interface DomainMigrationIntent {
at: number;
handoff_id?: string;
+ nonce?: string;
}
export function readDomainMigrationIntent(storage: StorageLike | null, now: number): DomainMigrationIntent | null {
@@ -360,7 +359,11 @@ export function readDomainMigrationIntent(storage: StorageLike | null, now: numb
if (!isRecord(value) || typeof value.at !== 'number' || now - value.at > DOMAIN_MIGRATION_PENDING_MAX_AGE_MS) {
return null;
}
- return typeof value.handoff_id === 'string' ? {at: value.at, handoff_id: value.handoff_id} : {at: value.at};
+ return {
+ at: value.at,
+ ...(typeof value.handoff_id === 'string' ? {handoff_id: value.handoff_id} : {}),
+ ...(typeof value.nonce === 'string' ? {nonce: value.nonce} : {}),
+ };
} catch {
return null;
}
@@ -393,7 +396,7 @@ export type DomainMigrationInstallKind =
export type DomainMigrationDisplayMode = 'browser' | 'minimal-ui' | 'standalone' | 'window-controls-overlay';
-export interface DomainMigrationInstallSignals {
+interface DomainMigrationInstallSignals {
displayMode: DomainMigrationDisplayMode;
navigatorStandalone: boolean;
userAgent: string;
@@ -706,10 +709,6 @@ export function parseDomainMigrationPayload(value: unknown, expectedSource: stri
};
}
-function exportableSnapshot(snapshot: Record<string, string> | undefined): Record<string, string> {
- return Object.fromEntries(Object.entries(snapshot ?? {}).filter(([key]) => isExportableLocalStorageKey(key)));
-}
-
export function keepValidTargetSession(incoming: StoredAccount, existing: StoredAccount | undefined): StoredAccount {
if (incoming.isValid !== false || !existing?.token || existing.isValid === false) {
return incoming;
@@ -721,13 +720,3 @@ export function keepValidTargetSession(incoming: StoredAccount, existing: Stored
userData: existing.userData ?? incoming.userData,
};
}
-
-export function rewriteImportedAccount(record: StoredAccount, instance: RuntimeConfigSnapshot): StoredAccount {
- const managed = exportableSnapshot(record.managedStorageData ?? record.localStorageData);
- return {
- ...record,
- localStorageData: managed,
- managedStorageData: managed,
- instance,
- };
-}
diff --git a/fluxer_app/src/features/app/domain_migration/DomainMigrationPreMount.ts b/fluxer_app/src/features/app/domain_migration/DomainMigrationPreMount.ts
index 2c83fd452..972308a8f 100644
--- a/fluxer_app/src/features/app/domain_migration/DomainMigrationPreMount.ts
+++ b/fluxer_app/src/features/app/domain_migration/DomainMigrationPreMount.ts
@@ -48,7 +48,6 @@ import {
readDomainMigrationIntent,
readDomainMigrationMarker,
resolveDomainMigrationSide,
- rewriteImportedAccount,
type StorageLike,
sanitizeNextPath,
shouldForwardCompletedSource,
@@ -66,11 +65,14 @@ import {
randomBase64Url,
sha256Hex,
} from '@app/features/app/domain_migration/DomainMigrationCrypto';
+import {runtimeInstanceKey} from '@app/features/app/state/InstanceSnapshotStore';
import type {RuntimeConfigSnapshot} from '@app/features/app/state/RuntimeConfig';
+import {parseAccountStorageKey} from '@app/features/auth/state/AccountStorageKey';
import type {SoundType} from '@app/features/notification/utils/SoundUtils';
import {
AuthSessionStorageKey,
parseStoredSessionValue,
+ readStoredActiveAccountKey,
readStoredSessionUserId,
} from '@app/features/platform/state/auth_session/AuthSessionStorage';
import {getProtectedLocalStorage, getProtectedSessionStorage} from '@app/features/platform/state/ProtectedWebStorage';
@@ -91,6 +93,9 @@ const MAX_HANDOFF_PAYLOAD_LENGTH = 8 * 1024 * 1024;
const IMPORT_WAIT_MS = 20_000;
const IMPORT_POLL_MS = 250;
const MEDIA_DEVICES_WAIT_MS = 1500;
+const REDEEM_RETRY_MAX_WAIT_MS = 60_000;
+const UNLOAD_GRACE_MS = 250;
+const MIGRATE_DISCOVERY_TIMEOUTS_MS: ReadonlyArray<number> = [5000, 15_000];
type DomainMigrationLanding = 'target' | 'source';
@@ -116,6 +121,11 @@ interface PendingHandoff {
nonce: string;
next: string;
at: number;
+ handoff?: string;
+ key?: string;
+ land?: string;
+ redeeming?: boolean;
+ sealed?: string;
}
function navigate(url: string): true {
@@ -123,6 +133,10 @@ function navigate(url: string): true {
return true;
}
+async function outliveUnload(): Promise<void> {
+ await new Promise((resolve) => setTimeout(resolve, UNLOAD_GRACE_MS));
+}
+
function mountInPlace(next: string): false {
window.history.replaceState(null, '', next);
return false;
@@ -301,18 +315,43 @@ async function createHandoff(apiEndpoint: string, token: string, nonceHash: stri
return body.handoff_id;
}
-async function redeemHandoff(target: string, handoffId: string, nonce: string): Promise<string> {
- let response: Response;
+async function rateLimitRetryMs(response: Response): Promise<number | null> {
try {
- response = await fetch(`${target}/api/v1/auth/origin-handoff/redeem`, {
- method: 'POST',
- credentials: 'omit',
- headers: {'Content-Type': 'application/json'},
- body: JSON.stringify({handoff_id: handoffId, nonce}),
- });
+ const body = (await response.json()) as {retry_after?: unknown};
+ return typeof body.retry_after === 'number' && body.retry_after >= 0 ? Math.ceil(body.retry_after * 1000) : null;
} catch {
- throw new DomainMigrationImportError('redeem_failed');
+ return null;
}
+}
+
+async function postRedeem(target: string, handoffId: string, nonce: string): Promise<Response> {
+ const deadline = Date.now() + REDEEM_RETRY_MAX_WAIT_MS;
+ for (;;) {
+ let response: Response;
+ try {
+ response = await fetch(`${target}/api/v1/auth/origin-handoff/redeem`, {
+ method: 'POST',
+ credentials: 'omit',
+ headers: {'Content-Type': 'application/json'},
+ body: JSON.stringify({handoff_id: handoffId, nonce}),
+ });
+ } catch {
+ throw new DomainMigrationImportError('redeem_failed');
+ }
+ if (response.status !== 429) {
+ return response;
+ }
+ const waitMs = await rateLimitRetryMs(response);
+ if (waitMs === null || Date.now() + waitMs > deadline) {
+ return response;
+ }
+ logger.warn(`Origin handoff redeem was rate limited, retrying in ${waitMs}ms`);
+ await new Promise((resolve) => setTimeout(resolve, waitMs));
+ }
+}
+
+async function redeemHandoff(target: string, handoffId: string, nonce: string): Promise<string> {
+ const response = await postRedeem(target, handoffId, nonce);
if (response.status === 404) {
throw new DomainMigrationImportError('handoff_expired');
}
@@ -350,7 +389,7 @@ async function exportFromSource(side: DomainMigrationSide, nonce: string | null)
const storage = getProtectedLocalStorage();
const sessionStorage = getProtectedSessionStorage();
const intent = readDomainMigrationIntent(sessionStorage, Date.now());
- if (intent === null || intent.handoff_id !== undefined) {
+ if (intent === null || (intent.handoff_id !== undefined && intent.nonce !== nonce)) {
clearDomainMigrationIntent(sessionStorage);
return navigate(sourceUrl(side, DOMAIN_MIGRATION_DEFAULT_NEXT_PATH));
}
@@ -402,7 +441,7 @@ async function exportFromSource(side: DomainMigrationSide, nonce: string | null)
sealed = await encryptDomainMigrationPayload(withoutOptionalPayloadData(payload));
}
const handoffId = await createHandoff(instance.apiEndpoint, token, await sha256Hex(nonce), sealed.payload);
- writeDomainMigrationIntent(sessionStorage, {at: intent.at, handoff_id: handoffId});
+ writeDomainMigrationIntent(sessionStorage, {at: intent.at, handoff_id: handoffId, nonce});
markDomainMigrationHandedOff(storage, side.target, Date.now(), attempts);
const landing: DomainMigrationLanding = environmentMayForward(
readDomainMigrationEnvironment(),
@@ -413,6 +452,7 @@ async function exportFromSource(side: DomainMigrationSide, nonce: string | null)
return navigate(`${side.target}/migrate/complete#h=${handoffId}&k=${sealed.key}&land=${landing}`);
} catch (err) {
logger.warn('Domain migration export failed:', err);
+ await outliveUnload();
clearDomainMigrationIntent(sessionStorage);
revokeCompletedMarker(storage);
return navigate(sourceUrl(side, DOMAIN_MIGRATION_DEFAULT_NEXT_PATH));
@@ -566,11 +606,13 @@ async function handleSource(side: DomainMigrationSide): Promise<boolean> {
return false;
}
-function takePendingHandoff(): PendingHandoff | null {
- const storage = getProtectedSessionStorage();
+function optionalString(value: unknown): string | undefined {
+ return typeof value === 'string' ? value : undefined;
+}
+
+function readPendingHandoff(): PendingHandoff | null {
try {
- const raw = storage?.getItem(DOMAIN_MIGRATION_PENDING_KEY) ?? null;
- storage?.removeItem(DOMAIN_MIGRATION_PENDING_KEY);
+ const raw = getProtectedSessionStorage()?.getItem(DOMAIN_MIGRATION_PENDING_KEY) ?? null;
if (!raw) {
return null;
}
@@ -578,19 +620,43 @@ function takePendingHandoff(): PendingHandoff | null {
if (typeof value.nonce !== 'string' || typeof value.next !== 'string' || typeof value.at !== 'number') {
return null;
}
- return {nonce: value.nonce, next: value.next, at: value.at};
+ return {
+ nonce: value.nonce,
+ next: value.next,
+ at: value.at,
+ handoff: optionalString(value.handoff),
+ key: optionalString(value.key),
+ land: optionalString(value.land),
+ redeeming: value.redeeming === true,
+ sealed: optionalString(value.sealed),
+ };
} catch {
return null;
}
}
-async function importHandoff(side: DomainMigrationSide, fragment: URLSearchParams, nonce: string): Promise<void> {
- const handoffId = fragment.get('h');
- const key = fragment.get('k');
+function writePendingHandoff(pending: PendingHandoff | null): void {
+ const storage = getProtectedSessionStorage();
+ try {
+ if (pending === null) {
+ storage?.removeItem(DOMAIN_MIGRATION_PENDING_KEY);
+ } else {
+ storage?.setItem(DOMAIN_MIGRATION_PENDING_KEY, JSON.stringify(pending));
+ }
+ } catch {}
+}
+
+async function importHandoff(side: DomainMigrationSide, pending: PendingHandoff): Promise<void> {
+ const {handoff: handoffId, key} = pending;
if (!handoffId || !key || !BASE64URL_TOKEN_PATTERN.test(handoffId)) {
throw new DomainMigrationImportError('missing_handoff');
}
- const sealed = await redeemHandoff(side.target, handoffId, nonce);
+ let sealed = pending.sealed;
+ if (sealed === undefined) {
+ writePendingHandoff({...pending, redeeming: true});
+ sealed = await redeemHandoff(side.target, handoffId, pending.nonce);
+ writePendingHandoff({...pending, redeeming: true, sealed});
+ }
let payload: DomainMigrationPayload | null;
try {
payload = parseDomainMigrationPayload(await decryptDomainMigrationPayload(sealed, key), side.source);
@@ -603,6 +669,10 @@ async function importHandoff(side: DomainMigrationSide, fragment: URLSearchParam
const storage = getProtectedLocalStorage();
const previousImport = readDomainMigrationImport(storage);
writeDomainMigrationImport(storage, {state: 'running', at: Date.now()});
+ const [appStorage, {getPersistentStorageBackend}] = await Promise.all([
+ import('@app/features/app/domain_migration/DomainMigrationAppStorage'),
+ import('@app/features/platform/state/PersistentStorageBackend'),
+ ]);
let instance: RuntimeConfigSnapshot;
try {
const [{default: accountStorage}, {runtimeConfigSnapshotsAreSameInstance}, snapshot] = await Promise.all([
@@ -622,7 +692,7 @@ async function importHandoff(side: DomainMigrationSide, fragment: URLSearchParam
);
await accountStorage.importAccounts(
payload.accounts.map((account) =>
- keepValidTargetSession(rewriteImportedAccount(account, instance), existing.get(account.userId)),
+ keepValidTargetSession(appStorage.rewriteImportedAccount(account, instance), existing.get(account.userId)),
),
);
if (activeToken !== null && activeUserId !== null && incomingToken !== null && incomingUserId !== activeUserId) {
@@ -647,15 +717,25 @@ async function importHandoff(side: DomainMigrationSide, fragment: URLSearchParam
}
if (payload.app_storage) {
try {
- const [{writeImportedAppStorage}, {getPersistentStorageBackend}] = await Promise.all([
- import('@app/features/app/domain_migration/DomainMigrationAppStorage'),
- import('@app/features/platform/state/PersistentStorageBackend'),
- ]);
- await writeImportedAppStorage(getPersistentStorageBackend(), payload.app_storage, payload.accounts, instance);
+ await appStorage.writeImportedAppStorage(
+ getPersistentStorageBackend(),
+ payload.app_storage,
+ payload.accounts,
+ instance,
+ );
} catch (err) {
logger.warn('Failed to import scoped app storage:', err);
}
}
+ try {
+ await appStorage.retargetAccountKeys(
+ getPersistentStorageBackend(),
+ storage,
+ appStorage.importedAccountKeyMoves(payload.accounts, instance),
+ );
+ } catch (err) {
+ logger.warn('Failed to point imported state at the new account keys:', err);
+ }
if (payload.media_devices) {
writeDomainMigrationDeviceMap(storage, {at: Date.now(), devices: payload.media_devices, resolved: []});
}
@@ -674,14 +754,19 @@ function doneUrl(side: DomainMigrationSide, next: string, handoffId: string | nu
return `${side.source}/migrate/done?${handoff}next=${encodeURIComponent(next)}`;
}
-function landingUrl(side: DomainMigrationSide, fragment: URLSearchParams, next: string): string {
- switch (fragment.get('land')) {
+function landOnTarget(
+ side: DomainMigrationSide,
+ land: string | undefined,
+ handoffId: string | null,
+ next: string,
+): boolean {
+ switch (land) {
case 'target':
- return `${side.target}${next}`;
+ return mountInPlace(next);
case 'source':
- return sourceUrl(side, next);
+ return navigate(sourceUrl(side, next));
}
- return doneUrl(side, next, fragment.get('h'));
+ return navigate(doneUrl(side, next, handoffId));
}
function failedUrl(side: DomainMigrationSide, reason: DomainMigrationFailureReason, next: string): string {
@@ -726,34 +811,73 @@ async function beginOnTarget(side: DomainMigrationSide, params: URLSearchParams)
async function completeOnTarget(side: DomainMigrationSide): Promise<boolean> {
const fragment = new URLSearchParams(window.location.hash.slice(1));
window.history.replaceState(window.history.state, '', `${window.location.pathname}${window.location.search}`);
- const pending = takePendingHandoff();
- if (pending === null || Date.now() - pending.at > DOMAIN_MIGRATION_PENDING_MAX_AGE_MS) {
- const lastImport = readDomainMigrationImport(getProtectedLocalStorage());
+ const stored = readPendingHandoff();
+ const lastImport = readDomainMigrationImport(getProtectedLocalStorage());
+ if (stored === null || Date.now() - stored.at > DOMAIN_MIGRATION_PENDING_MAX_AGE_MS) {
+ writePendingHandoff(null);
if (lastImport?.state === 'done' && Date.now() - lastImport.at < DOMAIN_MIGRATION_PENDING_MAX_AGE_MS) {
- return navigate(
- fragment.get('land') === 'source'
- ? sourceUrl(side, DOMAIN_MIGRATION_DEFAULT_NEXT_PATH)
- : `${side.target}${DOMAIN_MIGRATION_DEFAULT_NEXT_PATH}`,
- );
+ const land = fragment.get('land') === 'source' ? 'source' : 'target';
+ return landOnTarget(side, land, null, DOMAIN_MIGRATION_DEFAULT_NEXT_PATH);
}
return navigate(failedUrl(side, 'no_pending', DOMAIN_MIGRATION_DEFAULT_NEXT_PATH));
}
+ const pending: PendingHandoff = {
+ ...stored,
+ handoff: fragment.get('h') ?? stored.handoff,
+ key: fragment.get('k') ?? stored.key,
+ land: fragment.get('land') ?? stored.land,
+ };
+ writePendingHandoff(pending);
const next = sanitizeNextPath(pending.next);
+ if (lastImport?.state === 'done' && lastImport.at >= pending.at) {
+ writePendingHandoff(null);
+ return landOnTarget(side, pending.land, pending.handoff ?? null, next);
+ }
try {
- await importHandoff(side, fragment, pending.nonce);
+ await importHandoff(side, pending);
} catch (err) {
const reason = err instanceof DomainMigrationImportError ? err.reason : 'import_failed';
logger.warn('Domain migration import failed:', err);
+ await outliveUnload();
+ writePendingHandoff(null);
const storage = getProtectedLocalStorage();
if (readDomainMigrationImport(storage)?.state === 'running') {
writeDomainMigrationImport(storage, null);
}
+ if (reason === 'handoff_expired' && pending.redeeming === true) {
+ return navigate(`${side.source}/migrate/start?next=${encodeURIComponent(next)}`);
+ }
return navigate(failedUrl(side, reason, next));
}
- return navigate(landingUrl(side, fragment, next));
+ writePendingHandoff(null);
+ return landOnTarget(side, pending.land, pending.handoff ?? null, next);
+}
+
+async function repairMovedAccounts(): Promise<void> {
+ const storage = getProtectedLocalStorage();
+ if (storage === null || readDomainMigrationImport(storage)?.state !== 'done') {
+ return;
+ }
+ const instance = await currentInstanceSnapshot();
+ const pointer = parseAccountStorageKey(readStoredActiveAccountKey(storage));
+ if (pointer === null || pointer.instanceKey === runtimeInstanceKey(instance)) {
+ return;
+ }
+ const [{default: accountStorage}, {retargetAccountKeys}, {getPersistentStorageBackend}] = await Promise.all([
+ import('@app/features/auth/state/AccountStorage'),
+ import('@app/features/app/domain_migration/DomainMigrationAppStorage'),
+ import('@app/features/platform/state/PersistentStorageBackend'),
+ ]);
+ const moves = await accountStorage.rekeyAccountsOnInstance(instance);
+ await retargetAccountKeys(getPersistentStorageBackend(), storage, moves);
}
async function handleTarget(side: DomainMigrationSide): Promise<boolean> {
+ if (window.location.pathname !== '/migrate/complete') {
+ await repairMovedAccounts().catch((err: unknown) => {
+ logger.warn('Failed to repair accounts moved by an earlier import:', err);
+ });
+ }
switch (window.location.pathname) {
case '/migrate/begin':
return beginOnTarget(side, new URLSearchParams(window.location.search));
@@ -763,6 +887,20 @@ async function handleTarget(side: DomainMigrationSide): Promise<boolean> {
return false;
}
+async function loadDiscovery(): Promise<void> {
+ const timeouts: ReadonlyArray<number | undefined> = window.location.pathname.startsWith('/migrate/')
+ ? MIGRATE_DISCOVERY_TIMEOUTS_MS
+ : [undefined];
+ for (const [index, timeoutMs] of timeouts.entries()) {
+ try {
+ await loadDomainMigrationDiscovery(timeoutMs);
+ return;
+ } catch (err) {
+ logger.warn(`Domain migration discovery is unavailable (attempt ${index + 1} of ${timeouts.length}):`, err);
+ }
+ }
+}
+
export async function runDomainMigrationPreMount(): Promise<boolean> {
if (typeof window === 'undefined') {
return false;
@@ -771,11 +909,7 @@ export async function runDomainMigrationPreMount(): Promise<boolean> {
if (side === null) {
return false;
}
- try {
- await loadDomainMigrationDiscovery();
- } catch (err) {
- logger.warn('Domain migration discovery is unavailable:', err);
- }
+ await loadDiscovery();
try {
return side.role === 'source' ? await handleSource(side) : await handleTarget(side);
} catch (err) {