fix(expo): account linking flow on mobile (#5836)

This commit is contained in:
Alex
2025-11-07 21:45:00 +00:00
committed by GitHub
parent 77d10e0e6f
commit 12db1cb091
2 changed files with 36 additions and 1 deletions
+3 -1
View File
@@ -373,7 +373,9 @@ export const expoClient = (opts: ExpoClientOptions) => {
const url = new URL(result.url);
const cookie = String(url.searchParams.get("cookie"));
if (!cookie) return;
storage.setItem(cookieName, getSetCookie(cookie));
const prevCookie = await storage.getItem(cookieName);
const toSetCookie = getSetCookie(cookie, prevCookie ?? undefined);
storage.setItem(cookieName, toSetCookie);
store?.notify("$sessionSignal");
}
},
+33
View File
@@ -307,6 +307,39 @@ describe("expo", async () => {
});
expect(origin).toBe(null);
});
it("should preserve existing cookies on link-social", async () => {
await client.signIn.email({
email: "test@test.com",
password: "password",
});
const testCookie = "better-auth.test-key";
const testCookieValue = "abc";
const storedCookieBefore = storage.get("better-auth_cookie");
expect(storedCookieBefore).toBeDefined();
const parsedCookieBefore = JSON.parse(storedCookieBefore || "");
expect(parsedCookieBefore[testCookie]).toBeUndefined();
const expoWebBrowser = await import("expo-web-browser");
vi.mocked(expoWebBrowser.openAuthSessionAsync).mockResolvedValueOnce({
type: "success",
url: `better-auth://?cookie=${testCookie}=${testCookieValue}`,
});
await client.linkSocial({ provider: "google" });
const storedCookieAfter = storage.get("better-auth_cookie");
expect(storedCookieAfter).toBeDefined();
const parsedCookieAfter = JSON.parse(storedCookieAfter || "");
expect(parsedCookieAfter[testCookie]?.value).toBe(testCookieValue);
Object.keys(parsedCookieBefore).forEach((key) => {
expect(
parsedCookieAfter[key]?.value,
`cookie "${key}" value is preserved`,
).toBe(parsedCookieBefore[key]?.value);
});
});
});
describe("expo with cookieCache", async () => {