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

Allow setting domain when deleting cookies #5341

Merged
merged 8 commits into from
Dec 14, 2022
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/small-ravens-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Allow setting domain when deleting cookies
7 changes: 4 additions & 3 deletions packages/astro/src/core/cookies/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ interface AstroCookieSetOptions {
secure?: boolean;
}

interface AstroCookieDeleteOptions {
path?: string;
}
type AstroCookieDeleteOptions = Pick<AstroCookieSetOptions, 'domain' | 'path'>;

interface AstroCookieInterface {
value: string | undefined;
Expand Down Expand Up @@ -75,6 +73,9 @@ class AstroCookies implements AstroCookiesInterface {
expires: DELETED_EXPIRATION,
};

if (options?.domain) {
serializeOptions.domain = options.domain;
}
if (options?.path) {
serializeOptions.path = options.path;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/test/units/cookies/delete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,16 @@ describe('astro/src/core/cookies', () => {
expect(headers).to.have.a.lengthOf(1);
expect(headers[0]).to.match(/Path=\/subpath\//);
});

it('can provide a domain', () => {
let req = new Request('http://example.com/');
let cookies = new AstroCookies(req);
cookies.delete('foo', {
domain: '.example.com',
});
let headers = Array.from(cookies.headers());
expect(headers).to.have.a.lengthOf(1);
expect(headers[0]).to.match(/Domain=\.example\.com/);
});
});
});