react-token-manager 1.1.3 → 1.1.4

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/dist/index.d.mts CHANGED
@@ -17,13 +17,11 @@ declare class TokenManager {
17
17
  getAll(): Record<string, string | null>;
18
18
  /** Remove specific tokens */
19
19
  remove(keys: string | string[]): void;
20
- /** Clear all tracked auth tokens */
20
+ /** Clear all tracked tokens */
21
21
  clear(): void;
22
- /** Decode token */
22
+ /** Decode JWT token safely */
23
23
  decode<T = unknown>(token: string): T | null;
24
- /** Check if a token is expired
25
- * If no token is provided, defaults to checking 'access_token'
26
- */
24
+ /** Check if token is expired. Defaults to 'access_token' if no token is passed */
27
25
  isExpired(token?: string): boolean;
28
26
  }
29
27
 
package/dist/index.d.ts CHANGED
@@ -17,13 +17,11 @@ declare class TokenManager {
17
17
  getAll(): Record<string, string | null>;
18
18
  /** Remove specific tokens */
19
19
  remove(keys: string | string[]): void;
20
- /** Clear all tracked auth tokens */
20
+ /** Clear all tracked tokens */
21
21
  clear(): void;
22
- /** Decode token */
22
+ /** Decode JWT token safely */
23
23
  decode<T = unknown>(token: string): T | null;
24
- /** Check if a token is expired
25
- * If no token is provided, defaults to checking 'access_token'
26
- */
24
+ /** Check if token is expired. Defaults to 'access_token' if no token is passed */
27
25
  isExpired(token?: string): boolean;
28
26
  }
29
27
 
package/dist/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/index.ts
@@ -37,8 +27,7 @@ __export(index_exports, {
37
27
  module.exports = __toCommonJS(index_exports);
38
28
 
39
29
  // src/core.ts
40
- var jwtDecodeModule = __toESM(require("jwt-decode"));
41
- var jwtDecode = jwtDecodeModule.default || jwtDecodeModule;
30
+ var import_jwt_decode = require("jwt-decode");
42
31
  var globalOptions = {
43
32
  storage: "localStorage"
44
33
  };
@@ -56,7 +45,8 @@ var TokenManager = class {
56
45
  Object.entries(tokens).forEach(([key, value]) => {
57
46
  this.trackedKeys.add(key);
58
47
  if (this.storage === "localStorage") localStorage.setItem(key, value);
59
- else if (this.storage === "sessionStorage") sessionStorage.setItem(key, value);
48
+ else if (this.storage === "sessionStorage")
49
+ sessionStorage.setItem(key, value);
60
50
  else document.cookie = `${key}=${value}; path=/`;
61
51
  });
62
52
  }
@@ -95,7 +85,7 @@ var TokenManager = class {
95
85
  this.trackedKeys.delete(key);
96
86
  });
97
87
  }
98
- /** Clear all tracked auth tokens */
88
+ /** Clear all tracked tokens */
99
89
  clear() {
100
90
  this.trackedKeys.forEach((key) => {
101
91
  if (this.storage === "localStorage") localStorage.removeItem(key);
@@ -105,32 +95,28 @@ var TokenManager = class {
105
95
  });
106
96
  this.trackedKeys.clear();
107
97
  }
108
- /** Decode token */
98
+ /** Decode JWT token safely */
109
99
  decode(token) {
110
100
  if (!token) return null;
111
101
  try {
112
102
  const cleanToken = token.trim().replace(/^"|"$/g, "");
113
- return jwtDecode(cleanToken);
103
+ console.log("cleanToken", cleanToken);
104
+ return (0, import_jwt_decode.jwtDecode)(cleanToken);
114
105
  } catch (err) {
115
106
  console.error("JWT decode error:", err, token);
116
107
  return null;
117
108
  }
118
109
  }
119
- /** Check if a token is expired
120
- * If no token is provided, defaults to checking 'access_token'
121
- */
110
+ /** Check if token is expired. Defaults to 'access_token' if no token is passed */
122
111
  isExpired(token) {
123
112
  const tokenToCheck = token != null ? token : this.getOne("access_token");
124
- console.log("tokenToCheck:", tokenToCheck);
125
113
  if (!tokenToCheck) return true;
126
114
  const decoded = this.decode(tokenToCheck);
127
- console.log("decoded:", decoded);
128
115
  if (!decoded || !decoded.exp) return true;
116
+ console.log("passed");
129
117
  const now = Date.now();
130
118
  const expTime = decoded.exp * 1e3;
131
- const isExpired = now >= expTime;
132
- console.log("Token expires at:", new Date(expTime), "isExpired:", isExpired);
133
- return isExpired;
119
+ return now >= expTime;
134
120
  }
135
121
  };
136
122
 
package/dist/index.mjs CHANGED
@@ -1,6 +1,5 @@
1
1
  // src/core.ts
2
- import * as jwtDecodeModule from "jwt-decode";
3
- var jwtDecode = jwtDecodeModule.default || jwtDecodeModule;
2
+ import { jwtDecode } from "jwt-decode";
4
3
  var globalOptions = {
5
4
  storage: "localStorage"
6
5
  };
@@ -18,7 +17,8 @@ var TokenManager = class {
18
17
  Object.entries(tokens).forEach(([key, value]) => {
19
18
  this.trackedKeys.add(key);
20
19
  if (this.storage === "localStorage") localStorage.setItem(key, value);
21
- else if (this.storage === "sessionStorage") sessionStorage.setItem(key, value);
20
+ else if (this.storage === "sessionStorage")
21
+ sessionStorage.setItem(key, value);
22
22
  else document.cookie = `${key}=${value}; path=/`;
23
23
  });
24
24
  }
@@ -57,7 +57,7 @@ var TokenManager = class {
57
57
  this.trackedKeys.delete(key);
58
58
  });
59
59
  }
60
- /** Clear all tracked auth tokens */
60
+ /** Clear all tracked tokens */
61
61
  clear() {
62
62
  this.trackedKeys.forEach((key) => {
63
63
  if (this.storage === "localStorage") localStorage.removeItem(key);
@@ -67,32 +67,28 @@ var TokenManager = class {
67
67
  });
68
68
  this.trackedKeys.clear();
69
69
  }
70
- /** Decode token */
70
+ /** Decode JWT token safely */
71
71
  decode(token) {
72
72
  if (!token) return null;
73
73
  try {
74
74
  const cleanToken = token.trim().replace(/^"|"$/g, "");
75
+ console.log("cleanToken", cleanToken);
75
76
  return jwtDecode(cleanToken);
76
77
  } catch (err) {
77
78
  console.error("JWT decode error:", err, token);
78
79
  return null;
79
80
  }
80
81
  }
81
- /** Check if a token is expired
82
- * If no token is provided, defaults to checking 'access_token'
83
- */
82
+ /** Check if token is expired. Defaults to 'access_token' if no token is passed */
84
83
  isExpired(token) {
85
84
  const tokenToCheck = token != null ? token : this.getOne("access_token");
86
- console.log("tokenToCheck:", tokenToCheck);
87
85
  if (!tokenToCheck) return true;
88
86
  const decoded = this.decode(tokenToCheck);
89
- console.log("decoded:", decoded);
90
87
  if (!decoded || !decoded.exp) return true;
88
+ console.log("passed");
91
89
  const now = Date.now();
92
90
  const expTime = decoded.exp * 1e3;
93
- const isExpired = now >= expTime;
94
- console.log("Token expires at:", new Date(expTime), "isExpired:", isExpired);
95
- return isExpired;
91
+ return now >= expTime;
96
92
  }
97
93
  };
98
94
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-token-manager",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "A simple React library to manage JWT tokens",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
package/src/core.ts CHANGED
@@ -1,8 +1,5 @@
1
1
  import type { JwtPayload } from 'jwt-decode'
2
- import * as jwtDecodeModule from 'jwt-decode'
3
-
4
- const jwtDecode: <T = unknown>(token: string) => T =
5
- (jwtDecodeModule as any).default || jwtDecodeModule
2
+ import { jwtDecode } from 'jwt-decode'
6
3
 
7
4
  export type StorageType = 'localStorage' | 'sessionStorage' | 'cookie'
8
5
 
@@ -32,7 +29,8 @@ export class TokenManager {
32
29
  Object.entries(tokens).forEach(([key, value]) => {
33
30
  this.trackedKeys.add(key)
34
31
  if (this.storage === 'localStorage') localStorage.setItem(key, value)
35
- else if (this.storage === 'sessionStorage') sessionStorage.setItem(key, value)
32
+ else if (this.storage === 'sessionStorage')
33
+ sessionStorage.setItem(key, value)
36
34
  else document.cookie = `${key}=${value}; path=/`
37
35
  })
38
36
  }
@@ -41,11 +39,9 @@ export class TokenManager {
41
39
  get(keys: string | string[]): Record<string, string | null> {
42
40
  const keyArray = Array.isArray(keys) ? keys : [keys]
43
41
  const result: Record<string, string | null> = {}
44
-
45
42
  keyArray.forEach((key) => {
46
43
  result[key] = this.getOne(key)
47
44
  })
48
-
49
45
  return result
50
46
  }
51
47
 
@@ -69,18 +65,16 @@ export class TokenManager {
69
65
  /** Remove specific tokens */
70
66
  remove(keys: string | string[]) {
71
67
  const keyArray = Array.isArray(keys) ? keys : [keys]
72
-
73
68
  keyArray.forEach((key) => {
74
69
  if (this.storage === 'localStorage') localStorage.removeItem(key)
75
70
  else if (this.storage === 'sessionStorage') sessionStorage.removeItem(key)
76
71
  else
77
72
  document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
78
-
79
73
  this.trackedKeys.delete(key)
80
74
  })
81
75
  }
82
76
 
83
- /** Clear all tracked auth tokens */
77
+ /** Clear all tracked tokens */
84
78
  clear() {
85
79
  this.trackedKeys.forEach((key) => {
86
80
  if (this.storage === 'localStorage') localStorage.removeItem(key)
@@ -88,17 +82,16 @@ export class TokenManager {
88
82
  else
89
83
  document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
90
84
  })
91
-
92
85
  this.trackedKeys.clear()
93
86
  }
94
87
 
95
- /** Decode token */
88
+ /** Decode JWT token safely */
96
89
  decode<T = unknown>(token: string): T | null {
97
90
  if (!token) return null
98
-
99
91
  try {
100
- // Remove quotes and trim spaces
101
- const cleanToken = token.trim().replace(/^"|"$/g, '')
92
+ const cleanToken = token.trim().replace(/^"|"$/g, '') // Remove quotes
93
+ console.log('cleanToken', cleanToken);
94
+
102
95
  return jwtDecode<T>(cleanToken)
103
96
  } catch (err) {
104
97
  console.error('JWT decode error:', err, token)
@@ -106,29 +99,19 @@ export class TokenManager {
106
99
  }
107
100
  }
108
101
 
109
- /** Check if a token is expired
110
- * If no token is provided, defaults to checking 'access_token'
111
- */
102
+ /** Check if token is expired. Defaults to 'access_token' if no token is passed */
112
103
  isExpired(token?: string): boolean {
113
- // Use provided token, fallback to stored 'access_token'
114
104
  const tokenToCheck = token ?? this.getOne('access_token')
115
-
116
- console.log('tokenToCheck:', tokenToCheck)
117
-
118
105
  if (!tokenToCheck) return true
119
106
 
120
107
  const decoded = this.decode<JwtPayload>(tokenToCheck)
121
- console.log('decoded:', decoded)
122
-
123
- // If decoding fails or exp is missing, consider expired
124
108
  if (!decoded || !decoded.exp) return true
125
109
 
110
+ console.log('passed');
111
+
112
+
126
113
  const now = Date.now()
127
114
  const expTime = decoded.exp * 1000
128
- const isExpired = now >= expTime
129
-
130
- console.log('Token expires at:', new Date(expTime), 'isExpired:', isExpired)
131
- return isExpired
115
+ return now >= expTime
132
116
  }
133
-
134
117
  }