Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-8835: fix(auth): preserve a new bearer session after a stale 401 (#10209)

Danila Katalshove842560f1754
diff --git a/packages/core/api/client.test.ts b/packages/core/api/client.test.ts
index 1688829a09..5371db5cf5 100644
--- a/packages/core/api/client.test.ts
+++ b/packages/core/api/client.test.ts
@@ -3303,6 +3303,19 @@ describe("ApiClient shared credential across windows", () => {
expect(onUnauthorized).not.toHaveBeenCalled();
});
+ it("ignores a late 401 if a bearer session started after the request", async () => {
+ const onUnauthorized = vi.fn();
+ const client = new ApiClient("https://api.example.test", { onUnauthorized });
+ vi.stubGlobal("fetch", vi.fn(async () => {
+ client.setToken("signed-in");
+ return jsonResponse({ error: "unauthorized" }, 401);
+ }));
+
+ await expect(client.markOnboardingComplete()).rejects.toBeInstanceOf(ApiError);
+ expect(onUnauthorized).not.toHaveBeenCalled();
+ expect(client.getToken()).toBe("signed-in");
+ });
+
// ...but a genuine expiry still ends the session: nothing replaced the
// credential, so the 401 is about the one still in use.
it("still ends the session on a 401 for the credential in use", async () => {
diff --git a/packages/core/api/client.ts b/packages/core/api/client.ts
index d03ba569a8..f16d631d14 100644
--- a/packages/core/api/client.ts
+++ b/packages/core/api/client.ts
@@ -927,9 +927,14 @@ export class ApiClient {
* (MUL-7028). Comparing against the credential in use now is what tells the
* two apart: a genuinely expired session still has the same token stored,
* so the real case is unaffected.
+ *
+ * "Replaced" includes going from no credential to one. A flow that was
+ * already running at logout can still send a bearer-less request, and its
+ * 401 may land after the user has signed in again (#10190). Cookie mode is
+ * null on both sides, so its expiries still end the session.
*/
private handleUnauthorized(credentialUsed: string | null) {
- if (credentialUsed !== null && this.getToken() !== credentialUsed) {
+ if (this.getToken() !== credentialUsed) {
this.logger.info("ignoring 401 for a credential that has since been replaced");
return;
}