fix(expo): improve cookie expiration handling (#3705)

* fix: cookie expiration in expo

* changesets
This commit is contained in:
KinfeMichael Tariku
2025-08-01 02:32:54 +03:00
committed by GitHub
parent 685f70ae3d
commit 67f84d5d94
3 changed files with 16 additions and 11 deletions

View File

@@ -0,0 +1,5 @@
---
"@better-auth/expo": patch
---
fix(expo): improve cookie expiration handling

View File

@@ -50,7 +50,7 @@ interface ExpoClientOptions {
interface StoredCookie {
value: string;
expires: Date | null;
expires: string | null;
}
export function getSetCookie(header: string, prevCookie?: string) {
@@ -59,14 +59,14 @@ export function getSetCookie(header: string, prevCookie?: string) {
parsed.forEach((cookie, key) => {
const expiresAt = cookie["expires"];
const maxAge = cookie["max-age"];
const expires = expiresAt
? new Date(String(expiresAt))
: maxAge
? new Date(Date.now() + Number(maxAge))
const expires = maxAge
? new Date(Date.now() + Number(maxAge) * 1000)
: expiresAt
? new Date(String(expiresAt))
: null;
toSetCookie[key] = {
value: cookie["value"],
expires,
expires: expires ? expires.toISOString() : null,
};
});
if (prevCookie) {
@@ -89,7 +89,7 @@ export function getCookie(cookie: string) {
parsed = JSON.parse(cookie) as Record<string, StoredCookie>;
} catch (e) {}
const toSend = Object.entries(parsed).reduce((acc, [key, value]) => {
if (value.expires && value.expires < new Date()) {
if (value.expires && new Date(value.expires) < new Date()) {
return acc;
}
return `${acc}; ${key}=${value.value}`;

View File

@@ -199,11 +199,11 @@ describe("expo with cookieCache", async () => {
expect(storedCookie).toBeDefined();
const parsedCookie = JSON.parse(storedCookie || "");
expect(parsedCookie["better-auth.session_token"]).toMatchObject({
value: expect.stringMatching(/.+/),
value: expect.any(String),
expires: expect.any(String),
});
expect(parsedCookie["better-auth.session_data"]).toMatchObject({
value: expect.stringMatching(/.+/),
value: expect.any(String),
expires: expect.any(String),
});
});
@@ -216,11 +216,11 @@ describe("expo with cookieCache", async () => {
expect(storedCookie).toBeDefined();
const parsedCookie = JSON.parse(storedCookie || "");
expect(parsedCookie["better-auth.session_token"]).toMatchObject({
value: expect.stringMatching(/^$/),
value: expect.any(String),
expires: expect.any(String),
});
expect(parsedCookie["better-auth.session_data"]).toMatchObject({
value: expect.stringMatching(/^$/),
value: expect.any(String),
expires: expect.any(String),
});
});