mirror of
https://github.com/better-auth/better-auth.git
synced 2026-10-24 00:15:17 -05:00
fix(expo): account linking flow on mobile (#5836)
This commit is contained in:
@@ -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");
|
||||
}
|
||||
},
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user