Skip to content

Commit

Permalink
Allow setting domain when deleting cookies (#5341)
Browse files Browse the repository at this point in the history
* Allow setting domain when deleting cookies

* Add delete cookie with domain tests

* Fix syntax for AstroCookieDeleteOptions type

* Add changeset

* Update to a minor change rather than a patch

Co-authored-by: Matthew Phillips <matthew@skypack.dev>
  • Loading branch information
alexpdraper and matthewp authored Dec 14, 2022
1 parent dced4a8 commit 6b156dd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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/);
});
});
});

0 comments on commit 6b156dd

Please sign in to comment.