fix(web): show friendly error for invalid pairing token (#2222)
This commit is contained in:
1 parent
4ceabac764
commit
8f50ca8e2c
2 files changed
+63
-1
No files matched your search
@@ -353,6 +353,34 @@ describe("resolveInitialServerAuthGateState", () => {
|
||||
expect(fetchMock).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it("surfaces a friendly error message when an invalid pairing token is submitted", async () => {
|
||||
const fetchMock = vi.fn<typeof fetch>().mockResolvedValueOnce(
|
||||
jsonResponse(
|
||||
{
|
||||
error: "Invalid bootstrap credential.",
|
||||
},
|
||||
{
|
||||
status: 401,
|
||||
},
|
||||
),
|
||||
);
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
const { submitServerAuthCredential } = await import("./environments/primary");
|
||||
|
||||
await expect(submitServerAuthCredential("bad-token")).rejects.toThrow(
|
||||
"Invalid pairing token. Check the token and try again.",
|
||||
);
|
||||
expect(fetchMock).toHaveBeenCalledWith("http://localhost/api/auth/bootstrap", {
|
||||
body: JSON.stringify({ credential: "bad-token" }),
|
||||
credentials: "include",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
method: "POST",
|
||||
});
|
||||
});
|
||||
|
||||
it("waits for the authenticated session to become observable after silent desktop bootstrap", async () => {
|
||||
vi.useFakeTimers();
|
||||
const fetchMock = vi
|
||||
|
||||
@@ -110,6 +110,40 @@ async function readErrorMessage(response: Response, fallbackMessage: string): Pr
|
||||
return text || fallbackMessage;
|
||||
}
|
||||
|
||||
const INVALID_BOOTSTRAP_CREDENTIAL_MESSAGES = new Set([
|
||||
"Invalid bootstrap credential.",
|
||||
"Unknown bootstrap credential.",
|
||||
]);
|
||||
|
||||
function parseBootstrapErrorMessage(message: string): string {
|
||||
const trimmed = message.trim();
|
||||
if (trimmed.length === 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed) as {
|
||||
error?: unknown;
|
||||
};
|
||||
if (typeof parsed.error === "string" && parsed.error.trim().length > 0) {
|
||||
return parsed.error.trim();
|
||||
}
|
||||
} catch {
|
||||
// Not JSON; fall back to plain text.
|
||||
}
|
||||
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
function toFriendlyBootstrapErrorMessage(status: number, message: string): string {
|
||||
const parsedMessage = parseBootstrapErrorMessage(message);
|
||||
if (status === 401 && INVALID_BOOTSTRAP_CREDENTIAL_MESSAGES.has(parsedMessage)) {
|
||||
return "Invalid pairing token. Check the token and try again.";
|
||||
}
|
||||
|
||||
return parsedMessage;
|
||||
}
|
||||
|
||||
async function exchangeBootstrapCredential(credential: string): Promise<AuthBootstrapResult> {
|
||||
return retryTransientBootstrap(async () => {
|
||||
const payload: AuthBootstrapInput = { credential };
|
||||
@@ -123,7 +157,7 @@ async function exchangeBootstrapCredential(credential: string): Promise<AuthBoot
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
const message = toFriendlyBootstrapErrorMessage(response.status, await response.text());
|
||||
throw new BootstrapHttpError({
|
||||
message: message || `Failed to bootstrap auth session (${response.status}).`,
|
||||
status: response.status,
|
||||
|
||||
Reference in new issue
Block a user