Skip to content

Commit

Permalink
fix: pass cookie options to delete
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Dec 23, 2024
1 parent 5ab724c commit 657b2ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-lemons-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug that caused cookies to not be deleted when destroying a session
25 changes: 14 additions & 11 deletions packages/astro/src/core/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -259,15 +268,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);
}

/**
Expand Down Expand Up @@ -346,7 +349,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;
Expand Down
12 changes: 7 additions & 5 deletions packages/astro/test/units/sessions/astro-session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/');
});
});

Expand Down

0 comments on commit 657b2ff

Please sign in to comment.