react-token-manager 1.1.2 → 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 +3 -5
- package/dist/index.d.ts +3 -5
- package/dist/index.js +17 -34
- package/dist/index.mjs +17 -24
- package/package.json +1 -1
- package/src/core.ts +21 -44
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,35 +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
|
-
|
|
113
|
-
|
|
102
|
+
const cleanToken = token.trim().replace(/^"|"$/g, "");
|
|
103
|
+
console.log("cleanToken", cleanToken);
|
|
104
|
+
return (0, import_jwt_decode.jwtDecode)(cleanToken);
|
|
105
|
+
} catch (err) {
|
|
106
|
+
console.error("JWT decode error:", err, token);
|
|
114
107
|
return null;
|
|
115
108
|
}
|
|
116
109
|
}
|
|
117
|
-
/** Check if
|
|
118
|
-
* If no token is provided, defaults to checking 'access_token'
|
|
119
|
-
*/
|
|
110
|
+
/** Check if token is expired. Defaults to 'access_token' if no token is passed */
|
|
120
111
|
isExpired(token) {
|
|
121
112
|
const tokenToCheck = token != null ? token : this.getOne("access_token");
|
|
122
|
-
console.log("tokenToCheck", tokenToCheck);
|
|
123
113
|
if (!tokenToCheck) return true;
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const now = Date.now();
|
|
131
|
-
const expTime = decoded.exp * 1e3;
|
|
132
|
-
return now >= expTime;
|
|
133
|
-
} catch {
|
|
134
|
-
console.log("skipped");
|
|
135
|
-
return true;
|
|
136
|
-
}
|
|
114
|
+
const decoded = this.decode(tokenToCheck);
|
|
115
|
+
if (!decoded || !decoded.exp) return true;
|
|
116
|
+
console.log("passed");
|
|
117
|
+
const now = Date.now();
|
|
118
|
+
const expTime = decoded.exp * 1e3;
|
|
119
|
+
return now >= expTime;
|
|
137
120
|
}
|
|
138
121
|
};
|
|
139
122
|
|
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,35 +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
|
-
|
|
75
|
-
|
|
74
|
+
const cleanToken = token.trim().replace(/^"|"$/g, "");
|
|
75
|
+
console.log("cleanToken", cleanToken);
|
|
76
|
+
return jwtDecode(cleanToken);
|
|
77
|
+
} catch (err) {
|
|
78
|
+
console.error("JWT decode error:", err, token);
|
|
76
79
|
return null;
|
|
77
80
|
}
|
|
78
81
|
}
|
|
79
|
-
/** Check if
|
|
80
|
-
* If no token is provided, defaults to checking 'access_token'
|
|
81
|
-
*/
|
|
82
|
+
/** Check if token is expired. Defaults to 'access_token' if no token is passed */
|
|
82
83
|
isExpired(token) {
|
|
83
84
|
const tokenToCheck = token != null ? token : this.getOne("access_token");
|
|
84
|
-
console.log("tokenToCheck", tokenToCheck);
|
|
85
85
|
if (!tokenToCheck) return true;
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const now = Date.now();
|
|
93
|
-
const expTime = decoded.exp * 1e3;
|
|
94
|
-
return now >= expTime;
|
|
95
|
-
} catch {
|
|
96
|
-
console.log("skipped");
|
|
97
|
-
return true;
|
|
98
|
-
}
|
|
86
|
+
const decoded = this.decode(tokenToCheck);
|
|
87
|
+
if (!decoded || !decoded.exp) return true;
|
|
88
|
+
console.log("passed");
|
|
89
|
+
const now = Date.now();
|
|
90
|
+
const expTime = decoded.exp * 1e3;
|
|
91
|
+
return now >= expTime;
|
|
99
92
|
}
|
|
100
93
|
};
|
|
101
94
|
|
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,53 +82,36 @@ 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
91
|
try {
|
|
99
|
-
|
|
100
|
-
|
|
92
|
+
const cleanToken = token.trim().replace(/^"|"$/g, '') // Remove quotes
|
|
93
|
+
console.log('cleanToken', cleanToken);
|
|
94
|
+
|
|
95
|
+
return jwtDecode<T>(cleanToken)
|
|
96
|
+
} catch (err) {
|
|
97
|
+
console.error('JWT decode error:', err, token)
|
|
101
98
|
return null
|
|
102
99
|
}
|
|
103
100
|
}
|
|
104
101
|
|
|
105
|
-
/** Check if
|
|
106
|
-
* If no token is provided, defaults to checking 'access_token'
|
|
107
|
-
*/
|
|
102
|
+
/** Check if token is expired. Defaults to 'access_token' if no token is passed */
|
|
108
103
|
isExpired(token?: string): boolean {
|
|
109
|
-
|
|
110
|
-
const tokenToCheck = token ?? this.getOne('access_token');
|
|
111
|
-
|
|
112
|
-
console.log('tokenToCheck', tokenToCheck);
|
|
113
|
-
|
|
114
|
-
// If no token, consider it expired
|
|
104
|
+
const tokenToCheck = token ?? this.getOne('access_token')
|
|
115
105
|
if (!tokenToCheck) return true
|
|
116
|
-
|
|
117
|
-
try {
|
|
118
|
-
// Decode JWT safely
|
|
119
|
-
const decoded = this.decode<JwtPayload>(tokenToCheck)
|
|
120
106
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
// If decoding fails or exp is missing, token is expired
|
|
124
|
-
if (!decoded || !decoded.exp) return true
|
|
107
|
+
const decoded = this.decode<JwtPayload>(tokenToCheck)
|
|
108
|
+
if (!decoded || !decoded.exp) return true
|
|
125
109
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const expTime = decoded.exp * 1000 // exp is in seconds
|
|
133
|
-
return now >= expTime
|
|
134
|
-
} catch {
|
|
135
|
-
console.log('skipped');
|
|
136
|
-
// Any decoding error means token is invalid/expired
|
|
137
|
-
return true
|
|
138
|
-
}
|
|
110
|
+
console.log('passed');
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
const now = Date.now()
|
|
114
|
+
const expTime = decoded.exp * 1000
|
|
115
|
+
return now >= expTime
|
|
139
116
|
}
|
|
140
117
|
}
|