Skip to content

Commit

Permalink
Delete cookie without path attr before setting it
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Jul 11, 2024
1 parent 743c79e commit 710327d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ describe('keyValueStorage', () => {
);
});

it('should remove item before setting item', async () => {
const testKey = 'testKey';
const testValue = 'testValue';
keyValueStorage.setItem(testKey, testValue);
expect(mockCookiesStorageAdapter.delete).toHaveBeenCalledWith(testKey);
expect(mockCookiesStorageAdapter.set).toHaveBeenCalledWith(
testKey,
testValue,
{
...defaultSetCookieOptions,
expires: expect.any(Date),
},
);
});

it('should set item with options', async () => {
const testKey = 'testKey';
const testValue = 'testValue';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export const createKeyValueStorageFromCookieStorageAdapter = (
): KeyValueStorageInterface => {
return {
setItem(key, value) {
// Delete the cookie item first then set it. This results:
// SetCookie: key=;expires=1970-01-01;(path='current-path') <- remove path'ed cookies
// SetCookie: key=value;expires=Date.now() + 365 days;path=/;secure=true
cookieStorageAdapter.delete(key);

// TODO(HuiSF): follow up the default CookieSerializeOptions values
cookieStorageAdapter.set(key, value, {
...defaultSetCookieOptions,
Expand Down

0 comments on commit 710327d

Please sign in to comment.