fix: Cookies.set does not stringify JSON (#8807)
* fix: cookie setting * chore: remove debug statement * chore: add specs to test stringify
This commit is contained in:
@@ -27,7 +27,7 @@ export const getHeaderExpiry = response =>
|
|||||||
|
|
||||||
export const setAuthCredentials = response => {
|
export const setAuthCredentials = response => {
|
||||||
const expiryDate = getHeaderExpiry(response);
|
const expiryDate = getHeaderExpiry(response);
|
||||||
Cookies.set('cw_d_session_info', response.headers, {
|
Cookies.set('cw_d_session_info', JSON.stringify(response.headers), {
|
||||||
expires: differenceInDays(expiryDate, new Date()),
|
expires: differenceInDays(expiryDate, new Date()),
|
||||||
});
|
});
|
||||||
setUser(response.data.data, expiryDate);
|
setUser(response.data.data, expiryDate);
|
||||||
|
|||||||
@@ -34,5 +34,12 @@ export const setCookieWithDomain = (
|
|||||||
domain: baseDomain,
|
domain: baseDomain,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// if type of value is object, stringify it
|
||||||
|
// this is because js-cookies 3.0 removed builtin json support
|
||||||
|
// ref: https://github.com/js-cookie/js-cookie/releases/tag/v3.0.0
|
||||||
|
if (typeof value === 'object') {
|
||||||
|
value = JSON.stringify(value);
|
||||||
|
}
|
||||||
|
|
||||||
Cookies.set(name, value, cookieOptions);
|
Cookies.set(name, value, cookieOptions);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -107,6 +107,26 @@ describe('setCookieWithDomain', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should stringify the cookie value when setting', () => {
|
||||||
|
setCookieWithDomain(
|
||||||
|
'myCookie',
|
||||||
|
{ value: 'cookieValue' },
|
||||||
|
{
|
||||||
|
baseDomain: 'example.com',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(Cookies.set).toHaveBeenCalledWith(
|
||||||
|
'myCookie',
|
||||||
|
JSON.stringify({ value: 'cookieValue' }),
|
||||||
|
expect.objectContaining({
|
||||||
|
expires: 365,
|
||||||
|
sameSite: 'Lax',
|
||||||
|
domain: 'example.com',
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('should set a cookie with custom expiration, sameSite attribute, and specific base domain', () => {
|
it('should set a cookie with custom expiration, sameSite attribute, and specific base domain', () => {
|
||||||
setCookieWithDomain('myCookie', 'cookieValue', {
|
setCookieWithDomain('myCookie', 'cookieValue', {
|
||||||
expires: 7,
|
expires: 7,
|
||||||
|
|||||||
Reference in New Issue
Block a user