Skip to content

Commit

Permalink
Merge pull request #36 from js-smart/partitioned-flag
Browse files Browse the repository at this point in the history
feat(core): adds `partitioned` flag support
  • Loading branch information
pavankjadda authored Mar 26, 2024
2 parents a360ea8 + d0b35aa commit 4b806ed
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libs/js-cookie-service/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@js-smart/js-cookie-service",
"version": "2.4.0",
"version": "2.5.0",
"private": false,
"repository": {
"type": "git",
Expand Down
6 changes: 6 additions & 0 deletions libs/js-cookie-service/src/lib/js-cookie-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export function setCookie(
domain?: string;
secure?: boolean;
sameSite?: 'Lax' | 'None' | 'Strict';
partitioned?: boolean;
}
): void {
let cookieString: string =
Expand Down Expand Up @@ -153,6 +154,11 @@ export function setCookie(
cookieString += 'secure;';
}

// If `partitioned` flag is `true`, then set cookie partitioned flag
if (options?.partitioned) {
cookieString += 'Partitioned;';
}

// Set cookie sameSite attribute
cookieString += 'sameSite=' + (options?.sameSite ?? 'Lax') + ';';
document.cookie = cookieString;
Expand Down
3 changes: 2 additions & 1 deletion libs/react-cookie-service/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@js-smart/react-cookie-service",
"version": "2.4.0",
"version": "2.5.0",
"private": false,
"repository": {
"type": "git",
Expand All @@ -14,6 +14,7 @@
"react",
"react17",
"react18",
"react19",
"react-hooks",
"typescript",
"cookie",
Expand Down
6 changes: 6 additions & 0 deletions libs/react-cookie-service/src/lib/useCookies.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ describe('useCookies library tests', () => {
deleteCookie('token');
});

it('set token cookie with partitioned true', () => {
setCookie('token', token, { partitioned: true});
console.log('Partitioned cookie: ', getCookie('token'));
expect(getCookie('token')).toBe(token);
deleteCookie('token');
});

it('deletes existing X-Auth-Token cookie and cookie should not present', () => {
deleteCookie('X-Auth-Token');
Expand Down
7 changes: 7 additions & 0 deletions libs/react-cookie-service/src/lib/useCookies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export default function useCookies() {
* path Cookie path. Defaults to '/'
* domain Cookie domain
* secure Secure flag
* partitioned Partitioned flag
* sameSite OWASP same site token `Lax`, `None`, or `Strict`. Defaults to `Lax`
* </pre>
* @author Pavan Kumar Jadda
Expand All @@ -120,6 +121,7 @@ export default function useCookies() {
domain?: string;
secure?: boolean;
sameSite?: 'Lax' | 'None' | 'Strict';
partitioned?: boolean;
}
): void {
let cookieString: string =
Expand Down Expand Up @@ -158,6 +160,11 @@ export default function useCookies() {
cookieString += 'secure;';
}

// If `partitioned` flag is `true`, then set cookie partitioned flag
if (options?.partitioned) {
cookieString += 'Partitioned;';
}

// Set cookie sameSite attribute
cookieString += 'sameSite=' + (options?.sameSite ?? 'Lax') + ';';
document.cookie = cookieString;
Expand Down

0 comments on commit 4b806ed

Please sign in to comment.