mirror of
https://github.com/withastro/astro.git
synced 2024-12-30 22:03:56 -05:00
fix: pass cookie options to delete
This commit is contained in:
parent
5ab724c633
commit
657b2ffb23
3 changed files with 26 additions and 16 deletions
5
.changeset/heavy-lemons-tie.md
Normal file
5
.changeset/heavy-lemons-tie.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes a bug that caused cookies to not be deleted when destroying a session
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
|
|
@ -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, '/');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue