diff --git a/.changeset/heavy-lemons-tie.md b/.changeset/heavy-lemons-tie.md new file mode 100644 index 000000000000..666e0e2f8263 --- /dev/null +++ b/.changeset/heavy-lemons-tie.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a bug that caused cookies to not be deleted when destroying a session diff --git a/packages/astro/src/core/session.ts b/packages/astro/src/core/session.ts index 9ac7327c59e2..c0f013e07341 100644 --- a/packages/astro/src/core/session.ts +++ b/packages/astro/src/core/session.ts @@ -63,12 +63,21 @@ export class AstroSession { }: Exclude, undefined>, ) { this.#cookies = cookies; + let cookieConfigObject: AstroCookieSetOptions | undefined; if (typeof cookieConfig === 'object') { - this.#cookieConfig = cookieConfig; - this.#cookieName = cookieConfig.name || DEFAULT_COOKIE_NAME; + const { name = DEFAULT_COOKIE_NAME, ...rest } = cookieConfig; + this.#cookieName = name; + cookieConfigObject = rest; } else { this.#cookieName = cookieConfig || DEFAULT_COOKIE_NAME; } + this.#cookieConfig = { + sameSite: 'lax', + secure: true, + path: '/', + ...cookieConfigObject, + httpOnly: true, + }; this.#config = config; } @@ -259,15 +268,9 @@ export class AstroSession { message: 'Invalid cookie name. Cookie names can only contain letters, numbers, and dashes.', }); } - const cookieOptions: AstroCookieSetOptions = { - sameSite: 'lax', - secure: true, - path: '/', - ...this.#cookieConfig, - httpOnly: true, - }; + const value = this.#ensureSessionID(); - this.#cookies.set(this.#cookieName, value, cookieOptions); + this.#cookies.set(this.#cookieName, value, this.#cookieConfig); } /** @@ -346,7 +349,7 @@ export class AstroSession { this.#toDestroy.add(this.#sessionID); } if (this.#cookieName) { - this.#cookies.delete(this.#cookieName); + this.#cookies.delete(this.#cookieName, this.#cookieConfig); } this.#sessionID = undefined; this.#data = undefined; diff --git a/packages/astro/test/units/sessions/astro-session.test.js b/packages/astro/test/units/sessions/astro-session.test.js index 95a8b84ef528..3fa1b9de16bf 100644 --- a/packages/astro/test/units/sessions/astro-session.test.js +++ b/packages/astro/test/units/sessions/astro-session.test.js @@ -86,18 +86,20 @@ test('AstroSession - Cookie Management', async (t) => { }); await t.test('should delete cookie on destroy', async () => { - let cookieDeleted = false; + let cookieDeletedArgs; + let cookieDeletedName; const mockCookies = { ...defaultMockCookies, - delete: () => { - cookieDeleted = true; + delete: (name, args) => { + cookieDeletedName = name; + cookieDeletedArgs = args; }, }; const session = createSession(defaultConfig, mockCookies); session.destroy(); - - assert.equal(cookieDeleted, true); + assert.equal(cookieDeletedName, 'test-session'); + assert.equal(cookieDeletedArgs?.path, '/'); }); });