@oxyhq/core 1.11.6 → 1.11.7
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/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/AuthManager.js +22 -2
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/AuthManager.js +22 -2
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/AuthManager.d.ts +1 -1
- package/package.json +1 -1
- package/src/AuthManager.ts +22 -2
package/package.json
CHANGED
package/src/AuthManager.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type { OxyServices } from './OxyServices';
|
|
|
11
11
|
import type { HttpService } from './HttpService';
|
|
12
12
|
import type { SessionLoginResponse, MinimalUserData } from './models/session';
|
|
13
13
|
import { retryAsync } from './utils/asyncUtils';
|
|
14
|
+
import { jwtDecode } from 'jwt-decode';
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* OxyServices already declares revokeFedCMCredential via mixin type augmentation.
|
|
@@ -429,10 +430,29 @@ export class AuthManager {
|
|
|
429
430
|
}
|
|
430
431
|
|
|
431
432
|
/**
|
|
432
|
-
* Get
|
|
433
|
+
* Get a valid access token, refreshing automatically if expired or expiring soon.
|
|
433
434
|
*/
|
|
434
435
|
async getAccessToken(): Promise<string | null> {
|
|
435
|
-
|
|
436
|
+
const token = await this.storage.getItem(STORAGE_KEYS.ACCESS_TOKEN);
|
|
437
|
+
if (!token) return null;
|
|
438
|
+
|
|
439
|
+
try {
|
|
440
|
+
const decoded = jwtDecode<{ exp?: number }>(token);
|
|
441
|
+
if (decoded.exp) {
|
|
442
|
+
const now = Math.floor(Date.now() / 1000);
|
|
443
|
+
const buffer = 60; // refresh 60 seconds before expiry
|
|
444
|
+
if (decoded.exp - now < buffer) {
|
|
445
|
+
const refreshed = await this.refreshToken();
|
|
446
|
+
if (refreshed) {
|
|
447
|
+
return this.storage.getItem(STORAGE_KEYS.ACCESS_TOKEN);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
} catch {
|
|
452
|
+
// Decode failed — return token as-is, let the server decide
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return token;
|
|
436
456
|
}
|
|
437
457
|
|
|
438
458
|
/**
|