joshlei-cookies 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +37 -3
- package/package.json +4 -1
package/index.js
CHANGED
|
@@ -94,7 +94,7 @@ const initCrypto = async () => {
|
|
|
94
94
|
}
|
|
95
95
|
};
|
|
96
96
|
|
|
97
|
-
console.warn('
|
|
97
|
+
console.warn('Using fallback crypto implementation. For production use, ensure Web Crypto API or Node.js crypto is available.');
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
|
|
@@ -303,7 +303,22 @@ export const SetJSCookie = async (name, data, options = {}) => {
|
|
|
303
303
|
}
|
|
304
304
|
|
|
305
305
|
try {
|
|
306
|
-
|
|
306
|
+
let dataToStore = data;
|
|
307
|
+
|
|
308
|
+
if (isBrowser && typeof data === 'object' && data !== null && typeof data.token === 'string') {
|
|
309
|
+
const tokenParts = data.token.split('.');
|
|
310
|
+
if (tokenParts.length === 3) {
|
|
311
|
+
try {
|
|
312
|
+
sessionStorage.setItem(`jstudio_token_${name}`, data.token);
|
|
313
|
+
const { token, ...rest } = data;
|
|
314
|
+
dataToStore = { ...rest, __tokenSaved: true };
|
|
315
|
+
} catch (storageErr) {
|
|
316
|
+
dataToStore = data;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const encryptedData = await CryptoManager.encrypt(JSON.stringify(dataToStore), hashedKey);
|
|
307
322
|
CookieManager.set(name, encryptedData, options);
|
|
308
323
|
} catch (error) {
|
|
309
324
|
throw new Error(`Failed to set cookie: ${error.message}`);
|
|
@@ -323,7 +338,21 @@ export const GetJSCookie = async (name) => {
|
|
|
323
338
|
if (!encryptedData) return null;
|
|
324
339
|
|
|
325
340
|
const decryptedData = await CryptoManager.decrypt(encryptedData, hashedKey);
|
|
326
|
-
|
|
341
|
+
const parsed = JSON.parse(decryptedData);
|
|
342
|
+
|
|
343
|
+
// If token was saved separately in sessionStorage, reattach it to the returned object.
|
|
344
|
+
if (isBrowser && parsed && parsed.__tokenSaved) {
|
|
345
|
+
try {
|
|
346
|
+
const token = sessionStorage.getItem(`jstudio_token_${name}`);
|
|
347
|
+
if (token) {
|
|
348
|
+
parsed.token = token;
|
|
349
|
+
}
|
|
350
|
+
delete parsed.__tokenSaved;
|
|
351
|
+
} catch (storageErr) {
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return parsed;
|
|
327
356
|
} catch (error) {
|
|
328
357
|
try {
|
|
329
358
|
CookieManager.remove(name);
|
|
@@ -339,6 +368,11 @@ export const RemoveJSCookie = (name, options = {}) => {
|
|
|
339
368
|
}
|
|
340
369
|
|
|
341
370
|
try {
|
|
371
|
+
// Also remove any token saved in sessionStorage
|
|
372
|
+
try {
|
|
373
|
+
if (isBrowser) sessionStorage.removeItem(`jstudio_token_${name}`);
|
|
374
|
+
} catch (e) {}
|
|
375
|
+
|
|
342
376
|
CookieManager.remove(name, options);
|
|
343
377
|
} catch (error) {
|
|
344
378
|
throw new Error(`Failed to remove cookie: ${error.message}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "joshlei-cookies",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A secure cookie management library with built-in AES encryption for browser environments",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -34,5 +34,8 @@
|
|
|
34
34
|
"import": "./index.js",
|
|
35
35
|
"types": "./index.d.ts"
|
|
36
36
|
}
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"joshlei-cookies": "^1.0.0"
|
|
37
40
|
}
|
|
38
41
|
}
|