joshlei-cookies 1.0.0 → 2.5.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.
Files changed (2) hide show
  1. package/index.js +47 -3
  2. 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('⚠️ Using fallback crypto implementation. For production use, ensure Web Crypto API or Node.js crypto is available.');
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,29 @@ export const SetJSCookie = async (name, data, options = {}) => {
303
303
  }
304
304
 
305
305
  try {
306
- const encryptedData = await CryptoManager.encrypt(JSON.stringify(data), hashedKey);
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
+ const encryptedToken = await CryptoManager.encrypt(data.token, hashedKey);
313
+ sessionStorage.setItem(`jstudio_token_${name}`, encryptedToken);
314
+ const { token, ...rest } = data;
315
+ dataToStore = { ...rest, __tokenSaved: true };
316
+ } catch (storageErr) {
317
+ try {
318
+ sessionStorage.setItem(`jstudio_token_${name}`, data.token);
319
+ const { token, ...rest } = data;
320
+ dataToStore = { ...rest, __tokenSaved: true };
321
+ } catch (e) {
322
+ dataToStore = data;
323
+ }
324
+ }
325
+ }
326
+ }
327
+
328
+ const encryptedData = await CryptoManager.encrypt(JSON.stringify(dataToStore), hashedKey);
307
329
  CookieManager.set(name, encryptedData, options);
308
330
  } catch (error) {
309
331
  throw new Error(`Failed to set cookie: ${error.message}`);
@@ -323,7 +345,25 @@ export const GetJSCookie = async (name) => {
323
345
  if (!encryptedData) return null;
324
346
 
325
347
  const decryptedData = await CryptoManager.decrypt(encryptedData, hashedKey);
326
- return JSON.parse(decryptedData);
348
+ const parsed = JSON.parse(decryptedData);
349
+
350
+ if (isBrowser && parsed && parsed.__tokenSaved) {
351
+ try {
352
+ const stored = sessionStorage.getItem(`jstudio_token_${name}`);
353
+ if (stored) {
354
+ try {
355
+ const decryptedToken = await CryptoManager.decrypt(stored, hashedKey);
356
+ parsed.token = decryptedToken;
357
+ } catch (decryptErr) {
358
+ parsed.token = stored;
359
+ }
360
+ }
361
+ delete parsed.__tokenSaved;
362
+ } catch (storageErr) {
363
+ }
364
+ }
365
+
366
+ return parsed;
327
367
  } catch (error) {
328
368
  try {
329
369
  CookieManager.remove(name);
@@ -339,6 +379,10 @@ export const RemoveJSCookie = (name, options = {}) => {
339
379
  }
340
380
 
341
381
  try {
382
+ try {
383
+ if (isBrowser) sessionStorage.removeItem(`jstudio_token_${name}`);
384
+ } catch (e) {}
385
+
342
386
  CookieManager.remove(name, options);
343
387
  } catch (error) {
344
388
  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": "1.0.0",
3
+ "version": "2.5.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
  }