react-token-manager 1.0.6 → 1.0.8
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 +1 -1
- package/dist/index.js +10 -8
- package/dist/index.mjs +10 -8
- package/package.json +3 -2
- package/src/core.ts +20 -13
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -118,15 +118,17 @@ var TokenManager = class {
|
|
|
118
118
|
* If no token is provided, defaults to checking 'access_token'
|
|
119
119
|
*/
|
|
120
120
|
isExpired(token) {
|
|
121
|
-
|
|
122
|
-
if (!tokenToCheck) {
|
|
123
|
-
const stored = this.getOne("access_token");
|
|
124
|
-
tokenToCheck = stored || void 0;
|
|
125
|
-
}
|
|
121
|
+
const tokenToCheck = token != null ? token : this.getOne("access_token");
|
|
126
122
|
if (!tokenToCheck) return true;
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
123
|
+
try {
|
|
124
|
+
const decoded = this.decode(tokenToCheck);
|
|
125
|
+
if (!decoded || !decoded.exp) return true;
|
|
126
|
+
const now = Date.now();
|
|
127
|
+
const expTime = decoded.exp * 1e3;
|
|
128
|
+
return now >= expTime;
|
|
129
|
+
} catch {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
130
132
|
}
|
|
131
133
|
};
|
|
132
134
|
|
package/dist/index.mjs
CHANGED
|
@@ -80,15 +80,17 @@ var TokenManager = class {
|
|
|
80
80
|
* If no token is provided, defaults to checking 'access_token'
|
|
81
81
|
*/
|
|
82
82
|
isExpired(token) {
|
|
83
|
-
|
|
84
|
-
if (!tokenToCheck) {
|
|
85
|
-
const stored = this.getOne("access_token");
|
|
86
|
-
tokenToCheck = stored || void 0;
|
|
87
|
-
}
|
|
83
|
+
const tokenToCheck = token != null ? token : this.getOne("access_token");
|
|
88
84
|
if (!tokenToCheck) return true;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
85
|
+
try {
|
|
86
|
+
const decoded = this.decode(tokenToCheck);
|
|
87
|
+
if (!decoded || !decoded.exp) return true;
|
|
88
|
+
const now = Date.now();
|
|
89
|
+
const expTime = decoded.exp * 1e3;
|
|
90
|
+
return now >= expTime;
|
|
91
|
+
} catch {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
92
94
|
}
|
|
93
95
|
};
|
|
94
96
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-token-manager",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "A simple React library to manage JWT tokens",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -16,12 +16,13 @@
|
|
|
16
16
|
"react-dom": "^19.2.3"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
+
"@types/react-dom": "^19.2.3",
|
|
19
20
|
"js-cookie": "^3.0.5",
|
|
20
21
|
"jwt-decode": "^4.0.0"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
|
23
24
|
"@types/js-cookie": "^3.0.6",
|
|
24
|
-
"@types/react": "^
|
|
25
|
+
"@types/react": "^19.2.14",
|
|
25
26
|
"tsup": "^8.5.1",
|
|
26
27
|
"typescript": "^5.9.3"
|
|
27
28
|
}
|
package/src/core.ts
CHANGED
|
@@ -106,19 +106,26 @@ export class TokenManager {
|
|
|
106
106
|
* If no token is provided, defaults to checking 'access_token'
|
|
107
107
|
*/
|
|
108
108
|
isExpired(token?: string): boolean {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
tokenToCheck = stored || undefined
|
|
114
|
-
}
|
|
115
|
-
|
|
109
|
+
// Use provided token, fallback to stored 'access_token'
|
|
110
|
+
const tokenToCheck = token ?? this.getOne('access_token')
|
|
111
|
+
|
|
112
|
+
// If no token, consider it expired
|
|
116
113
|
if (!tokenToCheck) return true
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
// Decode JWT safely
|
|
117
|
+
const decoded = this.decode<JwtPayload>(tokenToCheck)
|
|
118
|
+
|
|
119
|
+
// If decoding fails or exp is missing, token is expired
|
|
120
|
+
if (!decoded || !decoded.exp) return true
|
|
121
|
+
|
|
122
|
+
// Compare current time with token expiry
|
|
123
|
+
const now = Date.now()
|
|
124
|
+
const expTime = decoded.exp * 1000 // exp is in seconds
|
|
125
|
+
return now >= expTime
|
|
126
|
+
} catch {
|
|
127
|
+
// Any decoding error means token is invalid/expired
|
|
128
|
+
return true
|
|
129
|
+
}
|
|
123
130
|
}
|
|
124
131
|
}
|