Merge pull request #19981 from strapi/chore/remove-js-cookie

chore: remove js-cookie dependency
This commit is contained in:
Christian 2024-04-15 19:21:14 +02:00 committed by GitHub
commit 281dd31953
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 54 additions and 21 deletions

View File

@ -1,11 +1,11 @@
import * as React from 'react';
import Cookies from 'js-cookie';
import { useIntl } from 'react-intl';
import { useNavigate, useMatch } from 'react-router-dom';
import { Page } from '../../../../admin/src/components/PageHelpers';
import { useAuth } from '../../../../admin/src/features/Auth';
import { getCookieValue, deleteCookie } from '../utils/cookies';
const AuthResponse = () => {
const match = useMatch('/auth/login/:authResponse');
@ -32,12 +32,12 @@ const AuthResponse = () => {
}
if (match?.params.authResponse === 'success') {
const jwtToken = Cookies.get('jwtToken');
const jwtToken = getCookieValue('jwtToken');
if (jwtToken) {
setToken(jwtToken);
Cookies.remove('jwtToken');
deleteCookie('jwtToken');
navigate('/auth/login');
} else {

View File

@ -0,0 +1,16 @@
export const getCookieValue = (name: string) => {
let result = null;
const cookieArray = document.cookie.split(';');
cookieArray.forEach((cookie) => {
const [key, value] = cookie.split('=').map((item) => item.trim());
if (key.trim() === name) {
result = decodeURIComponent(value);
}
});
return result;
};
export const deleteCookie = (name: string) => {
// Set the cookie to expire in the past
document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};

View File

@ -0,0 +1,35 @@
import { getCookieValue, deleteCookie } from '../src/utils/cookies';
describe('Cookie utils', () => {
beforeEach(() => {
document.cookie = 'cookie1=value1;';
document.cookie = 'cookie2=value2;';
document.cookie = 'cookie3=value3;';
});
describe('getCookieValue', () => {
it('should return the value of the specified cookie', () => {
expect(getCookieValue('cookie1')).toBe('value1');
expect(getCookieValue('cookie2')).toBe('value2');
expect(getCookieValue('cookie3')).toBe('value3');
});
it('should return null if the specified cookie does not exist', () => {
expect(getCookieValue('cookie4')).toBeNull();
expect(getCookieValue('cookie5')).toBeNull();
});
});
describe('deleteCookie', () => {
it('should delete the specified cookie', () => {
deleteCookie('cookie2');
expect(document.cookie).toBe('cookie1=value1; cookie3=value3');
});
it('should not delete any cookies if the specified cookie does not exist', () => {
deleteCookie('cookie4');
expect(document.cookie.split(';')).toEqual(
expect.arrayContaining('cookie1=value1; cookie2=value2; cookie3=value3'.split(';'))
);
});
});
});

View File

@ -104,7 +104,6 @@
"immer": "9.0.21",
"inquirer": "8.2.5",
"invariant": "^2.2.4",
"js-cookie": "2.2.1",
"jsonwebtoken": "9.0.0",
"koa": "2.15.2",
"koa-compose": "4.1.0",
@ -145,7 +144,6 @@
"@strapi/pack-up": "5.0.0",
"@types/codemirror5": "npm:@types/codemirror@^5.60.15",
"@types/invariant": "2.2.36",
"@types/js-cookie": "3.0.6",
"@types/jsonwebtoken": "9.0.3",
"@types/koa-passport": "6.0.1",
"@types/lodash": "^4.14.191",

View File

@ -7126,7 +7126,6 @@ __metadata:
"@testing-library/user-event": "npm:14.4.3"
"@types/codemirror5": "npm:@types/codemirror@^5.60.15"
"@types/invariant": "npm:2.2.36"
"@types/js-cookie": "npm:3.0.6"
"@types/jsonwebtoken": "npm:9.0.3"
"@types/koa-passport": "npm:6.0.1"
"@types/lodash": "npm:^4.14.191"
@ -7155,7 +7154,6 @@ __metadata:
immer: "npm:9.0.21"
inquirer: "npm:8.2.5"
invariant: "npm:^2.2.4"
js-cookie: "npm:2.2.1"
jsonwebtoken: "npm:9.0.0"
koa: "npm:2.15.2"
koa-body: "npm:6.0.1"
@ -9133,13 +9131,6 @@ __metadata:
languageName: node
linkType: hard
"@types/js-cookie@npm:3.0.6":
version: 3.0.6
resolution: "@types/js-cookie@npm:3.0.6"
checksum: 272d551687547445cb210213c73e72e0e5d58ad73e2e444a65d688b8ff9425529779ee0cd6492aaa1f070161916d4254ef2b1a76d64179100437f60749d094ef
languageName: node
linkType: hard
"@types/js-levenshtein@npm:^1.1.1":
version: 1.1.1
resolution: "@types/js-levenshtein@npm:1.1.1"
@ -19967,13 +19958,6 @@ __metadata:
languageName: node
linkType: hard
"js-cookie@npm:2.2.1":
version: 2.2.1
resolution: "js-cookie@npm:2.2.1"
checksum: 4387f5f5691cb96ca9ff8852c589d3012b53f484fda68630a39e20cabc6c5b740f09225e23233ba56cd9de6ebe300a23d20b2c7315f10c309ad5a89fd8c4990b
languageName: node
linkType: hard
"js-levenshtein@npm:^1.1.6":
version: 1.1.6
resolution: "js-levenshtein@npm:1.1.6"