Skip to content

Commit 892dd9f

Browse files
authored
fix: pass cookie options to delete (#12820)
1 parent 440d8a5 commit 892dd9f

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

.changeset/heavy-lemons-tie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a bug that caused cookies to not be deleted when destroying a session

packages/astro/src/core/session.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,21 @@ export class AstroSession<TDriver extends SessionDriverName = any> {
6363
}: Exclude<ResolvedSessionConfig<TDriver>, undefined>,
6464
) {
6565
this.#cookies = cookies;
66+
let cookieConfigObject: AstroCookieSetOptions | undefined;
6667
if (typeof cookieConfig === 'object') {
67-
this.#cookieConfig = cookieConfig;
68-
this.#cookieName = cookieConfig.name || DEFAULT_COOKIE_NAME;
68+
const { name = DEFAULT_COOKIE_NAME, ...rest } = cookieConfig;
69+
this.#cookieName = name;
70+
cookieConfigObject = rest;
6971
} else {
7072
this.#cookieName = cookieConfig || DEFAULT_COOKIE_NAME;
7173
}
74+
this.#cookieConfig = {
75+
sameSite: 'lax',
76+
secure: true,
77+
path: '/',
78+
...cookieConfigObject,
79+
httpOnly: true,
80+
};
7281
this.#config = config;
7382
}
7483

@@ -258,15 +267,9 @@ export class AstroSession<TDriver extends SessionDriverName = any> {
258267
message: 'Invalid cookie name. Cookie names can only contain letters, numbers, and dashes.',
259268
});
260269
}
261-
const cookieOptions: AstroCookieSetOptions = {
262-
sameSite: 'lax',
263-
secure: true,
264-
path: '/',
265-
...this.#cookieConfig,
266-
httpOnly: true,
267-
};
270+
268271
const value = this.#ensureSessionID();
269-
this.#cookies.set(this.#cookieName, value, cookieOptions);
272+
this.#cookies.set(this.#cookieName, value, this.#cookieConfig);
270273
}
271274

272275
/**
@@ -345,7 +348,7 @@ export class AstroSession<TDriver extends SessionDriverName = any> {
345348
this.#toDestroy.add(this.#sessionID);
346349
}
347350
if (this.#cookieName) {
348-
this.#cookies.delete(this.#cookieName);
351+
this.#cookies.delete(this.#cookieName, this.#cookieConfig);
349352
}
350353
this.#sessionID = undefined;
351354
this.#data = undefined;

packages/astro/test/units/sessions/astro-session.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,20 @@ test('AstroSession - Cookie Management', async (t) => {
8686
});
8787

8888
await t.test('should delete cookie on destroy', async () => {
89-
let cookieDeleted = false;
89+
let cookieDeletedArgs;
90+
let cookieDeletedName;
9091
const mockCookies = {
9192
...defaultMockCookies,
92-
delete: () => {
93-
cookieDeleted = true;
93+
delete: (name, args) => {
94+
cookieDeletedName = name;
95+
cookieDeletedArgs = args;
9496
},
9597
};
9698

9799
const session = createSession(defaultConfig, mockCookies);
98100
session.destroy();
99-
100-
assert.equal(cookieDeleted, true);
101+
assert.equal(cookieDeletedName, 'test-session');
102+
assert.equal(cookieDeletedArgs?.path, '/');
101103
});
102104
});
103105

0 commit comments

Comments
 (0)