mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-23 23:52:05 -05:00
fix(expo): improve cookie expiration handling (#3705)
* fix: cookie expiration in expo * changesets
This commit is contained in:
committed by
GitHub
parent
685f70ae3d
commit
67f84d5d94
5
.changeset/olive-toys-pump.md
Normal file
5
.changeset/olive-toys-pump.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@better-auth/expo": patch
|
||||
---
|
||||
|
||||
fix(expo): improve cookie expiration handling
|
||||
@@ -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}`;
|
||||
|
||||
@@ -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),
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user