react-token-manager 1.0.4 → 1.0.5

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
@@ -5,17 +5,22 @@ interface TokenManagerOptions {
5
5
  declare const configureTokenManager: (options: TokenManagerOptions) => void;
6
6
  declare class TokenManager {
7
7
  private storage;
8
+ private trackedKeys;
8
9
  constructor(options?: TokenManagerOptions);
9
10
  /** Set multiple tokens at once */
10
11
  set(tokens: Record<string, string>): void;
11
- /** Get single token by key or all tokens in an array of keys */
12
+ /** Get token(s) */
12
13
  get(keys: string | string[]): Record<string, string | null>;
13
- /** Remove multiple tokens by keys */
14
+ /** Remove specific tokens */
14
15
  remove(keys: string | string[]): void;
15
- /** Decode a single token by key */
16
+ /** Clear all tracked auth tokens */
17
+ clear(): void;
18
+ /** Decode token */
16
19
  decode<T = unknown>(token: string): T | null;
17
- /** Check if a token is expired */
18
- isExpired(token: string): boolean;
20
+ /** Check if a token is expired
21
+ * If no token is provided, defaults to checking 'access_token'
22
+ */
23
+ isExpired(token?: string): boolean;
19
24
  }
20
25
 
21
26
  declare const useTokenManager: () => {
@@ -25,6 +30,7 @@ declare const useTokenManager: () => {
25
30
  getTokens: (keys: string | string[]) => Record<string, string | null>;
26
31
  /** Remove multiple tokens by keys */
27
32
  removeTokens: (keys: string | string[]) => void;
33
+ clearTokens: () => void;
28
34
  /** Decode a single token */
29
35
  decodeToken: <T = unknown>(token: string) => T | null;
30
36
  /** Check expiration of a single token */
package/dist/index.d.ts CHANGED
@@ -5,17 +5,22 @@ interface TokenManagerOptions {
5
5
  declare const configureTokenManager: (options: TokenManagerOptions) => void;
6
6
  declare class TokenManager {
7
7
  private storage;
8
+ private trackedKeys;
8
9
  constructor(options?: TokenManagerOptions);
9
10
  /** Set multiple tokens at once */
10
11
  set(tokens: Record<string, string>): void;
11
- /** Get single token by key or all tokens in an array of keys */
12
+ /** Get token(s) */
12
13
  get(keys: string | string[]): Record<string, string | null>;
13
- /** Remove multiple tokens by keys */
14
+ /** Remove specific tokens */
14
15
  remove(keys: string | string[]): void;
15
- /** Decode a single token by key */
16
+ /** Clear all tracked auth tokens */
17
+ clear(): void;
18
+ /** Decode token */
16
19
  decode<T = unknown>(token: string): T | null;
17
- /** Check if a token is expired */
18
- isExpired(token: string): boolean;
20
+ /** Check if a token is expired
21
+ * If no token is provided, defaults to checking 'access_token'
22
+ */
23
+ isExpired(token?: string): boolean;
19
24
  }
20
25
 
21
26
  declare const useTokenManager: () => {
@@ -25,6 +30,7 @@ declare const useTokenManager: () => {
25
30
  getTokens: (keys: string | string[]) => Record<string, string | null>;
26
31
  /** Remove multiple tokens by keys */
27
32
  removeTokens: (keys: string | string[]) => void;
33
+ clearTokens: () => void;
28
34
  /** Decode a single token */
29
35
  decodeToken: <T = unknown>(token: string) => T | null;
30
36
  /** Check expiration of a single token */
package/dist/index.js CHANGED
@@ -47,18 +47,20 @@ var configureTokenManager = (options) => {
47
47
  };
48
48
  var TokenManager = class {
49
49
  constructor(options) {
50
+ this.trackedKeys = /* @__PURE__ */ new Set();
50
51
  const opts = options || globalOptions;
51
52
  this.storage = opts.storage || "localStorage";
52
53
  }
53
54
  /** Set multiple tokens at once */
54
55
  set(tokens) {
55
56
  Object.entries(tokens).forEach(([key, value]) => {
57
+ this.trackedKeys.add(key);
56
58
  if (this.storage === "localStorage") localStorage.setItem(key, value);
57
59
  else if (this.storage === "sessionStorage") sessionStorage.setItem(key, value);
58
60
  else document.cookie = `${key}=${value}; path=/`;
59
61
  });
60
62
  }
61
- /** Get single token by key or all tokens in an array of keys */
63
+ /** Get token(s) */
62
64
  get(keys) {
63
65
  const keyArray = Array.isArray(keys) ? keys : [keys];
64
66
  const result = {};
@@ -67,23 +69,37 @@ var TokenManager = class {
67
69
  if (this.storage === "localStorage") value = localStorage.getItem(key);
68
70
  else if (this.storage === "sessionStorage") value = sessionStorage.getItem(key);
69
71
  else {
70
- const match = document.cookie.match(new RegExp("(^| )" + key + "=([^;]+)"));
72
+ const match = document.cookie.match(
73
+ new RegExp("(^| )" + key + "=([^;]+)")
74
+ );
71
75
  value = match ? match[2] : null;
72
76
  }
73
77
  result[key] = value;
74
78
  });
75
79
  return result;
76
80
  }
77
- /** Remove multiple tokens by keys */
81
+ /** Remove specific tokens */
78
82
  remove(keys) {
79
83
  const keyArray = Array.isArray(keys) ? keys : [keys];
80
84
  keyArray.forEach((key) => {
81
85
  if (this.storage === "localStorage") localStorage.removeItem(key);
82
86
  else if (this.storage === "sessionStorage") sessionStorage.removeItem(key);
83
- else document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
87
+ else
88
+ document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
89
+ this.trackedKeys.delete(key);
84
90
  });
85
91
  }
86
- /** Decode a single token by key */
92
+ /** Clear all tracked auth tokens */
93
+ clear() {
94
+ this.trackedKeys.forEach((key) => {
95
+ if (this.storage === "localStorage") localStorage.removeItem(key);
96
+ else if (this.storage === "sessionStorage") sessionStorage.removeItem(key);
97
+ else
98
+ document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
99
+ });
100
+ this.trackedKeys.clear();
101
+ }
102
+ /** Decode token */
87
103
  decode(token) {
88
104
  if (!token) return null;
89
105
  try {
@@ -92,9 +108,17 @@ var TokenManager = class {
92
108
  return null;
93
109
  }
94
110
  }
95
- /** Check if a token is expired */
111
+ /** Check if a token is expired
112
+ * If no token is provided, defaults to checking 'access_token'
113
+ */
96
114
  isExpired(token) {
97
- const decoded = this.decode(token);
115
+ let tokenToCheck = token;
116
+ if (!tokenToCheck) {
117
+ const stored = this.get("access_token");
118
+ tokenToCheck = stored["access_token"] || void 0;
119
+ }
120
+ if (!tokenToCheck) return true;
121
+ const decoded = this.decode(tokenToCheck);
98
122
  if (!decoded || !decoded.exp) return true;
99
123
  return Date.now() >= decoded.exp * 1e3;
100
124
  }
@@ -111,6 +135,7 @@ var useTokenManager = () => {
111
135
  getTokens: (keys) => manager.get(keys),
112
136
  /** Remove multiple tokens by keys */
113
137
  removeTokens: (keys) => manager.remove(keys),
138
+ clearTokens: () => manager.clear(),
114
139
  /** Decode a single token */
115
140
  decodeToken: (token) => manager.decode(token),
116
141
  /** Check expiration of a single token */
package/dist/index.mjs CHANGED
@@ -9,18 +9,20 @@ var configureTokenManager = (options) => {
9
9
  };
10
10
  var TokenManager = class {
11
11
  constructor(options) {
12
+ this.trackedKeys = /* @__PURE__ */ new Set();
12
13
  const opts = options || globalOptions;
13
14
  this.storage = opts.storage || "localStorage";
14
15
  }
15
16
  /** Set multiple tokens at once */
16
17
  set(tokens) {
17
18
  Object.entries(tokens).forEach(([key, value]) => {
19
+ this.trackedKeys.add(key);
18
20
  if (this.storage === "localStorage") localStorage.setItem(key, value);
19
21
  else if (this.storage === "sessionStorage") sessionStorage.setItem(key, value);
20
22
  else document.cookie = `${key}=${value}; path=/`;
21
23
  });
22
24
  }
23
- /** Get single token by key or all tokens in an array of keys */
25
+ /** Get token(s) */
24
26
  get(keys) {
25
27
  const keyArray = Array.isArray(keys) ? keys : [keys];
26
28
  const result = {};
@@ -29,23 +31,37 @@ var TokenManager = class {
29
31
  if (this.storage === "localStorage") value = localStorage.getItem(key);
30
32
  else if (this.storage === "sessionStorage") value = sessionStorage.getItem(key);
31
33
  else {
32
- const match = document.cookie.match(new RegExp("(^| )" + key + "=([^;]+)"));
34
+ const match = document.cookie.match(
35
+ new RegExp("(^| )" + key + "=([^;]+)")
36
+ );
33
37
  value = match ? match[2] : null;
34
38
  }
35
39
  result[key] = value;
36
40
  });
37
41
  return result;
38
42
  }
39
- /** Remove multiple tokens by keys */
43
+ /** Remove specific tokens */
40
44
  remove(keys) {
41
45
  const keyArray = Array.isArray(keys) ? keys : [keys];
42
46
  keyArray.forEach((key) => {
43
47
  if (this.storage === "localStorage") localStorage.removeItem(key);
44
48
  else if (this.storage === "sessionStorage") sessionStorage.removeItem(key);
45
- else document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
49
+ else
50
+ document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
51
+ this.trackedKeys.delete(key);
46
52
  });
47
53
  }
48
- /** Decode a single token by key */
54
+ /** Clear all tracked auth tokens */
55
+ clear() {
56
+ this.trackedKeys.forEach((key) => {
57
+ if (this.storage === "localStorage") localStorage.removeItem(key);
58
+ else if (this.storage === "sessionStorage") sessionStorage.removeItem(key);
59
+ else
60
+ document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
61
+ });
62
+ this.trackedKeys.clear();
63
+ }
64
+ /** Decode token */
49
65
  decode(token) {
50
66
  if (!token) return null;
51
67
  try {
@@ -54,9 +70,17 @@ var TokenManager = class {
54
70
  return null;
55
71
  }
56
72
  }
57
- /** Check if a token is expired */
73
+ /** Check if a token is expired
74
+ * If no token is provided, defaults to checking 'access_token'
75
+ */
58
76
  isExpired(token) {
59
- const decoded = this.decode(token);
77
+ let tokenToCheck = token;
78
+ if (!tokenToCheck) {
79
+ const stored = this.get("access_token");
80
+ tokenToCheck = stored["access_token"] || void 0;
81
+ }
82
+ if (!tokenToCheck) return true;
83
+ const decoded = this.decode(tokenToCheck);
60
84
  if (!decoded || !decoded.exp) return true;
61
85
  return Date.now() >= decoded.exp * 1e3;
62
86
  }
@@ -73,6 +97,7 @@ var useTokenManager = () => {
73
97
  getTokens: (keys) => manager.get(keys),
74
98
  /** Remove multiple tokens by keys */
75
99
  removeTokens: (keys) => manager.remove(keys),
100
+ clearTokens: () => manager.clear(),
76
101
  /** Decode a single token */
77
102
  decodeToken: (token) => manager.decode(token),
78
103
  /** Check expiration of a single token */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-token-manager",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
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
@@ -76,7 +76,7 @@ export class TokenManager {
76
76
  }
77
77
 
78
78
  /** Clear all tracked auth tokens */
79
- clearAll() {
79
+ clear() {
80
80
  this.trackedKeys.forEach((key) => {
81
81
  if (this.storage === 'localStorage') localStorage.removeItem(key)
82
82
  else if (this.storage === 'sessionStorage') sessionStorage.removeItem(key)
package/src/hook.ts CHANGED
@@ -14,7 +14,7 @@ export const useTokenManager = () => {
14
14
  /** Remove multiple tokens by keys */
15
15
  removeTokens: (keys: string | string[]) => manager.remove(keys),
16
16
 
17
- clearTokens: () => manager.clearAll(),
17
+ clearTokens: () => manager.clear(),
18
18
 
19
19
  /** Decode a single token */
20
20
  decodeToken: <T = unknown>(token: string) => manager.decode<T>(token),