react-token-manager 1.0.3 → 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/README.md CHANGED
@@ -27,7 +27,14 @@ configureTokenManager({
27
27
  import { useTokenManager } from 'react-token-manager'
28
28
 
29
29
  export default function Dashboard() {
30
- const { setTokens, getTokens, removeTokens, decodeToken, isExpired } = useTokenManager()
30
+ const {
31
+ setTokens,
32
+ getTokens,
33
+ removeTokens,
34
+ clearAllTokens,
35
+ decodeToken,
36
+ isExpired
37
+ } = useTokenManager()
31
38
 
32
39
  // Set multiple tokens at once
33
40
  setTokens({
@@ -49,6 +56,9 @@ export default function Dashboard() {
49
56
  // Remove multiple tokens
50
57
  removeTokens(['access_token', 'refresh_token'])
51
58
 
59
+ // Clear all tracked tokens
60
+ clearAllTokens()
61
+
52
62
  return (
53
63
  <div>
54
64
  <p>Access Token: {tokens.access_token}</p>
@@ -77,20 +87,21 @@ manager.set({
77
87
  // Get multiple tokens
78
88
  const tokens = manager.get(['access_token', 'refresh_token'])
79
89
  console.log(tokens.access_token) // 'abc123'
80
- console.log(tokens.refresh_token) // 'xyz789'
81
90
 
82
91
  // Remove multiple tokens
83
92
  manager.remove(['access_token', 'refresh_token'])
84
93
 
85
- // Decode and check expiration of a single token
94
+ // Clear all tracked tokens
95
+ manager.clearAll()
96
+
97
+ // Decode and check expiration
86
98
  console.log(manager.decode(tokens.access_token!))
87
99
  console.log(manager.isExpired(tokens.access_token!))
100
+
88
101
  ```
89
102
 
90
103
  ## API Reference
91
104
 
92
- configureTokenManager(options: TokenManagerOptions)
93
-
94
105
  Set global options.
95
106
 
96
107
  ```bash
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.3",
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
@@ -20,6 +20,7 @@ export const configureTokenManager = (options: TokenManagerOptions) => {
20
20
 
21
21
  export class TokenManager {
22
22
  private storage: StorageType
23
+ private trackedKeys: Set<string> = new Set()
23
24
 
24
25
  constructor(options?: TokenManagerOptions) {
25
26
  const opts = options || globalOptions
@@ -29,42 +30,64 @@ export class TokenManager {
29
30
  /** Set multiple tokens at once */
30
31
  set(tokens: Record<string, string>) {
31
32
  Object.entries(tokens).forEach(([key, value]) => {
33
+ this.trackedKeys.add(key)
34
+
32
35
  if (this.storage === 'localStorage') localStorage.setItem(key, value)
33
36
  else if (this.storage === 'sessionStorage') sessionStorage.setItem(key, value)
34
37
  else document.cookie = `${key}=${value}; path=/`
35
38
  })
36
39
  }
37
40
 
38
- /** Get single token by key or all tokens in an array of keys */
41
+ /** Get token(s) */
39
42
  get(keys: string | string[]): Record<string, string | null> {
40
43
  const keyArray = Array.isArray(keys) ? keys : [keys]
41
44
  const result: Record<string, string | null> = {}
42
45
 
43
46
  keyArray.forEach((key) => {
44
47
  let value: string | null = null
48
+
45
49
  if (this.storage === 'localStorage') value = localStorage.getItem(key)
46
50
  else if (this.storage === 'sessionStorage') value = sessionStorage.getItem(key)
47
51
  else {
48
- const match = document.cookie.match(new RegExp('(^| )' + key + '=([^;]+)'))
52
+ const match = document.cookie.match(
53
+ new RegExp('(^| )' + key + '=([^;]+)')
54
+ )
49
55
  value = match ? match[2] : null
50
56
  }
57
+
51
58
  result[key] = value
52
59
  })
53
60
 
54
61
  return result
55
62
  }
56
63
 
57
- /** Remove multiple tokens by keys */
64
+ /** Remove specific tokens */
58
65
  remove(keys: string | string[]) {
59
66
  const keyArray = Array.isArray(keys) ? keys : [keys]
67
+
60
68
  keyArray.forEach((key) => {
61
69
  if (this.storage === 'localStorage') localStorage.removeItem(key)
62
70
  else if (this.storage === 'sessionStorage') sessionStorage.removeItem(key)
63
- else document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
71
+ else
72
+ document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
73
+
74
+ this.trackedKeys.delete(key)
75
+ })
76
+ }
77
+
78
+ /** Clear all tracked auth tokens */
79
+ clear() {
80
+ this.trackedKeys.forEach((key) => {
81
+ if (this.storage === 'localStorage') localStorage.removeItem(key)
82
+ else if (this.storage === 'sessionStorage') sessionStorage.removeItem(key)
83
+ else
84
+ document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
64
85
  })
86
+
87
+ this.trackedKeys.clear()
65
88
  }
66
89
 
67
- /** Decode a single token by key */
90
+ /** Decode token */
68
91
  decode<T = unknown>(token: string): T | null {
69
92
  if (!token) return null
70
93
  try {
@@ -74,10 +97,25 @@ export class TokenManager {
74
97
  }
75
98
  }
76
99
 
77
- /** Check if a token is expired */
78
- isExpired(token: string): boolean {
79
- const decoded = this.decode<JwtPayload>(token)
100
+ /** Check if a token is expired
101
+ * If no token is provided, defaults to checking 'access_token'
102
+ */
103
+ isExpired(token?: string): boolean {
104
+ let tokenToCheck = token
105
+
106
+ // If no token passed, try to get access_token
107
+ if (!tokenToCheck) {
108
+ const stored = this.get('access_token')
109
+ tokenToCheck = stored['access_token'] || undefined
110
+ }
111
+
112
+ if (!tokenToCheck) return true
113
+
114
+ const decoded = this.decode<JwtPayload>(tokenToCheck)
115
+
80
116
  if (!decoded || !decoded.exp) return true
117
+
81
118
  return Date.now() >= decoded.exp * 1000
82
119
  }
120
+
83
121
  }
package/src/hook.ts CHANGED
@@ -14,6 +14,8 @@ export const useTokenManager = () => {
14
14
  /** Remove multiple tokens by keys */
15
15
  removeTokens: (keys: string | string[]) => manager.remove(keys),
16
16
 
17
+ clearTokens: () => manager.clear(),
18
+
17
19
  /** Decode a single token */
18
20
  decodeToken: <T = unknown>(token: string) => manager.decode<T>(token),
19
21