react-token-manager 1.1.3 → 1.1.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 +3 -5
- package/dist/index.d.ts +3 -5
- package/dist/index.js +8 -25
- package/dist/index.mjs +7 -14
- package/package.json +1 -1
- package/src/core.ts +9 -31
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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")
|
|
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
|
|
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,25 @@ 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
|
+
return (0, import_jwt_decode.jwtDecode)(cleanToken);
|
|
114
104
|
} catch (err) {
|
|
115
|
-
console.error("JWT decode error:", err, token);
|
|
116
105
|
return null;
|
|
117
106
|
}
|
|
118
107
|
}
|
|
119
|
-
/** Check if
|
|
120
|
-
* If no token is provided, defaults to checking 'access_token'
|
|
121
|
-
*/
|
|
108
|
+
/** Check if token is expired. Defaults to 'access_token' if no token is passed */
|
|
122
109
|
isExpired(token) {
|
|
123
110
|
const tokenToCheck = token != null ? token : this.getOne("access_token");
|
|
124
|
-
console.log("tokenToCheck:", tokenToCheck);
|
|
125
111
|
if (!tokenToCheck) return true;
|
|
126
112
|
const decoded = this.decode(tokenToCheck);
|
|
127
|
-
console.log("decoded:", decoded);
|
|
128
113
|
if (!decoded || !decoded.exp) return true;
|
|
129
114
|
const now = Date.now();
|
|
130
115
|
const expTime = decoded.exp * 1e3;
|
|
131
|
-
|
|
132
|
-
console.log("Token expires at:", new Date(expTime), "isExpired:", isExpired);
|
|
133
|
-
return isExpired;
|
|
116
|
+
return now >= expTime;
|
|
134
117
|
}
|
|
135
118
|
};
|
|
136
119
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// src/core.ts
|
|
2
|
-
import
|
|
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")
|
|
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
|
|
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,25 @@ 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
75
|
return jwtDecode(cleanToken);
|
|
76
76
|
} catch (err) {
|
|
77
|
-
console.error("JWT decode error:", err, token);
|
|
78
77
|
return null;
|
|
79
78
|
}
|
|
80
79
|
}
|
|
81
|
-
/** Check if
|
|
82
|
-
* If no token is provided, defaults to checking 'access_token'
|
|
83
|
-
*/
|
|
80
|
+
/** Check if token is expired. Defaults to 'access_token' if no token is passed */
|
|
84
81
|
isExpired(token) {
|
|
85
82
|
const tokenToCheck = token != null ? token : this.getOne("access_token");
|
|
86
|
-
console.log("tokenToCheck:", tokenToCheck);
|
|
87
83
|
if (!tokenToCheck) return true;
|
|
88
84
|
const decoded = this.decode(tokenToCheck);
|
|
89
|
-
console.log("decoded:", decoded);
|
|
90
85
|
if (!decoded || !decoded.exp) return true;
|
|
91
86
|
const now = Date.now();
|
|
92
87
|
const expTime = decoded.exp * 1e3;
|
|
93
|
-
|
|
94
|
-
console.log("Token expires at:", new Date(expTime), "isExpired:", isExpired);
|
|
95
|
-
return isExpired;
|
|
88
|
+
return now >= expTime;
|
|
96
89
|
}
|
|
97
90
|
};
|
|
98
91
|
|
package/package.json
CHANGED
package/src/core.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import type { JwtPayload } from 'jwt-decode'
|
|
2
|
-
import
|
|
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')
|
|
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
|
|
77
|
+
/** Clear all tracked tokens */
|
|
84
78
|
clear() {
|
|
85
79
|
this.trackedKeys.forEach((key) => {
|
|
86
80
|
if (this.storage === 'localStorage') localStorage.removeItem(key)
|
|
@@ -88,47 +82,31 @@ 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
|
|
101
|
-
|
|
92
|
+
const cleanToken = token.trim().replace(/^"|"$/g, '') // Remove quotes
|
|
93
|
+
|
|
102
94
|
return jwtDecode<T>(cleanToken)
|
|
103
95
|
} catch (err) {
|
|
104
|
-
console.error('JWT decode error:', err, token)
|
|
105
96
|
return null
|
|
106
97
|
}
|
|
107
98
|
}
|
|
108
99
|
|
|
109
|
-
/** Check if
|
|
110
|
-
* If no token is provided, defaults to checking 'access_token'
|
|
111
|
-
*/
|
|
100
|
+
/** Check if token is expired. Defaults to 'access_token' if no token is passed */
|
|
112
101
|
isExpired(token?: string): boolean {
|
|
113
|
-
// Use provided token, fallback to stored 'access_token'
|
|
114
102
|
const tokenToCheck = token ?? this.getOne('access_token')
|
|
115
|
-
|
|
116
|
-
console.log('tokenToCheck:', tokenToCheck)
|
|
117
|
-
|
|
118
103
|
if (!tokenToCheck) return true
|
|
119
104
|
|
|
120
105
|
const decoded = this.decode<JwtPayload>(tokenToCheck)
|
|
121
|
-
console.log('decoded:', decoded)
|
|
122
|
-
|
|
123
|
-
// If decoding fails or exp is missing, consider expired
|
|
124
106
|
if (!decoded || !decoded.exp) return true
|
|
125
107
|
|
|
126
108
|
const now = Date.now()
|
|
127
109
|
const expTime = decoded.exp * 1000
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
console.log('Token expires at:', new Date(expTime), 'isExpired:', isExpired)
|
|
131
|
-
return isExpired
|
|
110
|
+
return now >= expTime
|
|
132
111
|
}
|
|
133
|
-
|
|
134
112
|
}
|