diff options
author | 2025-01-02 11:19:09 +0000 | |
---|---|---|
committer | 2025-01-02 11:19:09 +0000 | |
commit | 892dd9f6cd3935ce1d4f4dec523b248c2d15da12 (patch) | |
tree | 0c2ee47f20b615449aed3d148fac725573577dec | |
parent | 440d8a54f7b3d75dd16decb7d9d29e3724bff394 (diff) | |
download | astro-892dd9f6cd3935ce1d4f4dec523b248c2d15da12.tar.gz astro-892dd9f6cd3935ce1d4f4dec523b248c2d15da12.tar.zst astro-892dd9f6cd3935ce1d4f4dec523b248c2d15da12.zip |
fix: pass cookie options to delete (#12820)
-rw-r--r-- | .changeset/heavy-lemons-tie.md | 5 | ||||
-rw-r--r-- | packages/astro/src/core/session.ts | 25 | ||||
-rw-r--r-- | packages/astro/test/units/sessions/astro-session.test.js | 12 |
3 files changed, 26 insertions, 16 deletions
diff --git a/.changeset/heavy-lemons-tie.md b/.changeset/heavy-lemons-tie.md new file mode 100644 index 000000000..666e0e2f8 --- /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 33117a47a..9b5e4eb9e 100644 --- a/packages/astro/src/core/session.ts +++ b/packages/astro/src/core/session.ts @@ -63,12 +63,21 @@ export class AstroSession<TDriver extends SessionDriverName = any> { }: Exclude<ResolvedSessionConfig<TDriver>, 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; } @@ -258,15 +267,9 @@ export class AstroSession<TDriver extends SessionDriverName = any> { 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); } /** @@ -345,7 +348,7 @@ export class AstroSession<TDriver extends SessionDriverName = any> { 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 95a8b84ef..3fa1b9de1 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, '/'); }); }); |