fix(capacitor): validate mobile connection on app resume

Checks the runtime session before restoring a mobile connection
Disconnects and resets state when the session is no longer valid
Adds tests for reachable, unreachable, and unauthenticated runtimes
This commit is contained in:
Bohdan Triapitsyn
2026-07-05 23:51:11 +03:00
parent ec61cf3573
commit a6edc7baee
3 changed files with 123 additions and 6 deletions
+25
View File
@@ -452,6 +452,31 @@ export const autoConnectLastInstance = async (): Promise<boolean> => {
return true;
};
export const validateMobileConnectionSession = async (input: {
url: string;
clientToken?: string | null;
}): Promise<boolean> => {
let url = '';
try {
url = normalizeConnectionUrl(input.url);
} catch {
return false;
}
if (!url) return false;
const token = input.clientToken?.trim() || undefined;
const headers = token ? { Authorization: `Bearer ${token}` } : undefined;
const health = await requestWithTimeout(`${url}/health`, { method: 'GET', headers });
if (!health?.ok) return false;
const session = await requestWithTimeout(`${url}/auth/session`, { method: 'GET', credentials: 'include', headers });
if (!session || (!session.ok && session.status !== 404)) return false;
const status = await readSessionStatus(session);
return !(status && status.disabled !== true && status.authenticated === false);
};
// ---------------------------------------------------------------------------
// Shared connection controller
// ---------------------------------------------------------------------------