@spidy092/auth-client 1.0.13 โ 1.0.15
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/core.js +21 -21
- package/index.js +2 -1
- package/package.json +1 -1
- package/token.js +42 -2
- package/utils/jwt.js +9 -0
package/core.js
CHANGED
|
@@ -212,26 +212,26 @@ export async function refreshToken() {
|
|
|
212
212
|
|
|
213
213
|
|
|
214
214
|
|
|
215
|
-
export async function refreshToken() {
|
|
216
|
-
|
|
215
|
+
// export async function refreshToken() {
|
|
216
|
+
// const { clientKey, authBaseUrl } = getConfig();
|
|
217
217
|
|
|
218
|
-
|
|
218
|
+
// console.log('๐ Refreshing token:', { clientKey, mode: isRouterMode() ? 'ROUTER' : 'CLIENT' });
|
|
219
219
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}
|
|
220
|
+
// try {
|
|
221
|
+
// const response = await fetch(`${authBaseUrl}/refresh/${clientKey}`, {
|
|
222
|
+
// method: 'POST',
|
|
223
|
+
// credentials: 'include',
|
|
224
|
+
// });
|
|
225
|
+
|
|
226
|
+
// if (!response.ok) {
|
|
227
|
+
// throw new Error('Refresh failed');
|
|
228
|
+
// }
|
|
229
|
+
|
|
230
|
+
// const { access_token } = await response.json();
|
|
231
|
+
// setToken(access_token);
|
|
232
|
+
// return access_token;
|
|
233
|
+
// } catch (err) {
|
|
234
|
+
// clearToken();
|
|
235
|
+
// throw err;
|
|
236
|
+
// }
|
|
237
|
+
// }
|
package/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { setConfig, getConfig, isRouterMode } from './config';
|
|
|
3
3
|
import { login, logout, handleCallback, refreshToken, resetCallbackState } from './core';
|
|
4
4
|
import { getToken, setToken, clearToken, addTokenListener, removeTokenListener, getListenerCount } from './token';
|
|
5
5
|
import api from './api';
|
|
6
|
-
import { decodeToken, isTokenExpired } from './utils/jwt';
|
|
6
|
+
import { decodeToken, isTokenExpired, isAuthenticated } from './utils/jwt';
|
|
7
7
|
|
|
8
8
|
export const auth = {
|
|
9
9
|
// ๐ง Config
|
|
@@ -32,6 +32,7 @@ export const auth = {
|
|
|
32
32
|
// ๐งช Utilities
|
|
33
33
|
decodeToken,
|
|
34
34
|
isTokenExpired,
|
|
35
|
+
isAuthenticated,
|
|
35
36
|
|
|
36
37
|
// ๐ Auto-refresh setup
|
|
37
38
|
startTokenRefresh: () => {
|
package/package.json
CHANGED
package/token.js
CHANGED
|
@@ -96,6 +96,46 @@ export function removeTokenListener(listener) {
|
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
// โ
Debug function to see current listeners
|
|
99
|
-
|
|
100
|
-
|
|
99
|
+
// โ
Decode JWT payload safely
|
|
100
|
+
// export function decodeToken(token) {
|
|
101
|
+
// if (!token) return null;
|
|
102
|
+
|
|
103
|
+
// try {
|
|
104
|
+
// const base64Url = token.split('.')[1]; // JWT payload is 2nd part
|
|
105
|
+
// if (!base64Url) return null;
|
|
106
|
+
|
|
107
|
+
// const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
|
|
108
|
+
// const jsonPayload = decodeURIComponent(
|
|
109
|
+
// atob(base64)
|
|
110
|
+
// .split('')
|
|
111
|
+
// .map((c) => `%${('00' + c.charCodeAt(0).toString(16)).slice(-2)}`)
|
|
112
|
+
// .join('')
|
|
113
|
+
// );
|
|
114
|
+
|
|
115
|
+
// return JSON.parse(jsonPayload);
|
|
116
|
+
// } catch (err) {
|
|
117
|
+
// console.warn('Could not decode token:', err);
|
|
118
|
+
// return null;
|
|
119
|
+
// }
|
|
120
|
+
// }
|
|
121
|
+
|
|
122
|
+
// // โ
Check if JWT is expired (with optional buffer)
|
|
123
|
+
// export function isTokenExpired(token, bufferSeconds = 0) {
|
|
124
|
+
// if (!token) return true;
|
|
125
|
+
|
|
126
|
+
// const decoded = decodeToken(token);
|
|
127
|
+
// if (!decoded || !decoded.exp) return true; // no exp claim โ treat as expired
|
|
128
|
+
|
|
129
|
+
// const expiryTime = decoded.exp * 1000; // exp is in seconds โ convert to ms
|
|
130
|
+
// const currentTime = Date.now();
|
|
131
|
+
|
|
132
|
+
// // Add buffer (e.g., 60s) to expire slightly earlier
|
|
133
|
+
// return currentTime >= expiryTime - bufferSeconds * 1000;
|
|
134
|
+
// }
|
|
135
|
+
|
|
136
|
+
// โ
Check if user is authenticated
|
|
137
|
+
export function isAuthenticated() {
|
|
138
|
+
const token = getToken();
|
|
139
|
+
return !!token && !isTokenExpired(token);
|
|
101
140
|
}
|
|
141
|
+
|
package/utils/jwt.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// auth-client/utils/jwt.js
|
|
2
2
|
import { jwtDecode } from 'jwt-decode';
|
|
3
|
+
import { getToken } from '../token';
|
|
3
4
|
|
|
4
5
|
export function decodeToken(token) {
|
|
5
6
|
try {
|
|
@@ -16,3 +17,11 @@ export function isTokenExpired(token, bufferSeconds = 60) {
|
|
|
16
17
|
const currentTime = Date.now() / 1000;
|
|
17
18
|
return decoded.exp < currentTime + bufferSeconds;
|
|
18
19
|
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
// โ
Check if user is authenticated
|
|
23
|
+
export function isAuthenticated() {
|
|
24
|
+
const token = getToken();
|
|
25
|
+
return !!token && !isTokenExpired(token);
|
|
26
|
+
}
|
|
27
|
+
|