Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass cookie options to delete #12820

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};
ematipico marked this conversation as resolved.
Show resolved Hide resolved
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
Loading