react-token-manager 1.0.7 → 1.0.9
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.js +11 -8
- package/dist/index.mjs +11 -8
- package/package.json +1 -1
- package/src/core.ts +22 -12
package/dist/index.js
CHANGED
|
@@ -118,15 +118,18 @@ var TokenManager = class {
|
|
|
118
118
|
* If no token is provided, defaults to checking 'access_token'
|
|
119
119
|
*/
|
|
120
120
|
isExpired(token) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const stored = this.getOne("access_token");
|
|
124
|
-
tokenToCheck = stored || void 0;
|
|
125
|
-
}
|
|
121
|
+
const tokenToCheck = token != null ? token : this.getOne("access_token");
|
|
122
|
+
console.log("tokenToCheck", tokenToCheck);
|
|
126
123
|
if (!tokenToCheck) return true;
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
124
|
+
try {
|
|
125
|
+
const decoded = this.decode(tokenToCheck);
|
|
126
|
+
if (!decoded || !decoded.exp) return true;
|
|
127
|
+
const now = Date.now();
|
|
128
|
+
const expTime = decoded.exp * 1e3;
|
|
129
|
+
return now >= expTime;
|
|
130
|
+
} catch {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
130
133
|
}
|
|
131
134
|
};
|
|
132
135
|
|
package/dist/index.mjs
CHANGED
|
@@ -80,15 +80,18 @@ var TokenManager = class {
|
|
|
80
80
|
* If no token is provided, defaults to checking 'access_token'
|
|
81
81
|
*/
|
|
82
82
|
isExpired(token) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const stored = this.getOne("access_token");
|
|
86
|
-
tokenToCheck = stored || void 0;
|
|
87
|
-
}
|
|
83
|
+
const tokenToCheck = token != null ? token : this.getOne("access_token");
|
|
84
|
+
console.log("tokenToCheck", tokenToCheck);
|
|
88
85
|
if (!tokenToCheck) return true;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
86
|
+
try {
|
|
87
|
+
const decoded = this.decode(tokenToCheck);
|
|
88
|
+
if (!decoded || !decoded.exp) return true;
|
|
89
|
+
const now = Date.now();
|
|
90
|
+
const expTime = decoded.exp * 1e3;
|
|
91
|
+
return now >= expTime;
|
|
92
|
+
} catch {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
92
95
|
}
|
|
93
96
|
};
|
|
94
97
|
|
package/package.json
CHANGED
package/src/core.ts
CHANGED
|
@@ -106,19 +106,29 @@ 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
|
-
if (!tokenToCheck) {
|
|
112
|
-
const stored = this.getOne('access_token')
|
|
113
|
-
tokenToCheck = stored || undefined
|
|
114
|
-
}
|
|
109
|
+
// Use provided token, fallback to stored 'access_token'
|
|
110
|
+
const tokenToCheck = token ?? this.getOne('access_token');
|
|
115
111
|
|
|
112
|
+
console.log('tokenToCheck', tokenToCheck);
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
// If no token, consider it expired
|
|
116
116
|
if (!tokenToCheck) return true
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
// Decode JWT safely
|
|
120
|
+
const decoded = this.decode<JwtPayload>(tokenToCheck)
|
|
121
|
+
|
|
122
|
+
// If decoding fails or exp is missing, token is expired
|
|
123
|
+
if (!decoded || !decoded.exp) return true
|
|
124
|
+
|
|
125
|
+
// Compare current time with token expiry
|
|
126
|
+
const now = Date.now()
|
|
127
|
+
const expTime = decoded.exp * 1000 // exp is in seconds
|
|
128
|
+
return now >= expTime
|
|
129
|
+
} catch {
|
|
130
|
+
// Any decoding error means token is invalid/expired
|
|
131
|
+
return true
|
|
132
|
+
}
|
|
123
133
|
}
|
|
124
134
|
}
|