@spacelr/sdk 0.10.11 → 0.11.0
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 +83 -6
- package/dist/index.d.ts +83 -6
- package/dist/index.js +92 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +92 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -372,6 +372,12 @@ interface UploadProgress {
|
|
|
372
372
|
}
|
|
373
373
|
|
|
374
374
|
type RefreshCallback = (refreshToken: string) => Promise<StoredTokens>;
|
|
375
|
+
/**
|
|
376
|
+
* Reasons the SDK reports a **server-confirmed** auth loss via `onAuthLost`.
|
|
377
|
+
* A transient refresh failure (network/transport OR a 5xx) is NOT reported
|
|
378
|
+
* (#759/#762).
|
|
379
|
+
* See the `onAuthLost` JSDoc in `client.ts` for the full contract.
|
|
380
|
+
*/
|
|
375
381
|
type AuthLostReason = 'refresh-failed' | 'unauthorized';
|
|
376
382
|
type TokenRefreshedListener = (tokens: StoredTokens) => void;
|
|
377
383
|
type AuthLostListener = (reason: AuthLostReason) => void;
|
|
@@ -384,6 +390,7 @@ declare class TokenManager {
|
|
|
384
390
|
private tokenRefreshedListeners;
|
|
385
391
|
private authLostListeners;
|
|
386
392
|
private authLostEmitted;
|
|
393
|
+
private pendingClear;
|
|
387
394
|
constructor(storage?: TokenStorage, refreshBufferSeconds?: number, refreshLockName?: string);
|
|
388
395
|
setRefreshCallback(callback: RefreshCallback): void;
|
|
389
396
|
getAccessToken(): Promise<string | null>;
|
|
@@ -398,9 +405,52 @@ declare class TokenManager {
|
|
|
398
405
|
* rather than an exception that would get wrapped as a network error.
|
|
399
406
|
*/
|
|
400
407
|
forceRefresh(): Promise<string | null>;
|
|
408
|
+
/**
|
|
409
|
+
* Whether the stored session currently carries a refresh token. Used by the
|
|
410
|
+
* 401-recovery path to decide whether a refresh was even possible, so it can
|
|
411
|
+
* let `doRefresh` own the auth-loss classification instead of second-guessing
|
|
412
|
+
* it. A rejecting storage backend reads as "no refresh token".
|
|
413
|
+
*/
|
|
414
|
+
hasRefreshToken(): Promise<boolean>;
|
|
401
415
|
onTokenRefreshed(listener: TokenRefreshedListener): () => void;
|
|
402
416
|
onAuthLost(listener: AuthLostListener): () => void;
|
|
403
417
|
emitAuthLost(reason: AuthLostReason): void;
|
|
418
|
+
private dispatchAuthLost;
|
|
419
|
+
/**
|
|
420
|
+
* Handle a server-confirmed auth loss: drop the dead session, THEN notify.
|
|
421
|
+
*
|
|
422
|
+
* The stored tokens are cleared **before** listeners run (#759) so that a
|
|
423
|
+
* reload lands on the login screen and `hasStoredSession()` reads false — and,
|
|
424
|
+
* critically, so a listener that re-logs-in via `setTokens()` (synchronously
|
|
425
|
+
* OR after an await) writes its new session strictly AFTER the clear and can
|
|
426
|
+
* never be erased by it. The clear is a storage-only wipe: we do NOT reset
|
|
427
|
+
* `authLostEmitted` (as `clearTokens()` would), so the emit stays deduped for
|
|
428
|
+
* this lost session. A rejecting storage backend is swallowed — best-effort,
|
|
429
|
+
* the consumer is routed to logout regardless. `refreshPromise` is left to
|
|
430
|
+
* `doRefresh`'s own `finally`; nulling it here could clobber a newer in-flight
|
|
431
|
+
* refresh.
|
|
432
|
+
*
|
|
433
|
+
* The token-clearing auth-loss path. Called from `doRefresh` (single-flighted
|
|
434
|
+
* via `refreshPromise`) on a server-confirmed refresh rejection, and from
|
|
435
|
+
* `HttpClient` only when a 401 could not be refreshed AND no refresh was even
|
|
436
|
+
* attempted (no refresh token) — these are mutually exclusive, so there is
|
|
437
|
+
* exactly one clear per loss and it runs BEFORE any listener. A re-login a
|
|
438
|
+
* listener performs is therefore never erased.
|
|
439
|
+
*/
|
|
440
|
+
notifyAuthLost(reason: AuthLostReason): Promise<void>;
|
|
441
|
+
/**
|
|
442
|
+
* A refresh failure that is transient (retryable), not a server-confirmed auth
|
|
443
|
+
* loss — so it must NOT eject a local-first app; the next request retries.
|
|
444
|
+
* Covers:
|
|
445
|
+
* - a transport failure (offline, tunnel, captive portal, unreachable
|
|
446
|
+
* gateway) surfaced as `SpacelrNetworkError`/`SpacelrTimeoutError` (#759);
|
|
447
|
+
* - a 5xx server response — e.g. a 503 the gateway returns when oauth-service
|
|
448
|
+
* is momentarily down (#760/#762) — which is retryable infrastructure, not
|
|
449
|
+
* a token rejection.
|
|
450
|
+
* A real auth rejection (401/invalid_grant) or any other non-5xx server error
|
|
451
|
+
* is NOT transient and is treated as a server-confirmed loss.
|
|
452
|
+
*/
|
|
453
|
+
private isTransientTransportError;
|
|
404
454
|
private isTokenExpired;
|
|
405
455
|
private shouldRefresh;
|
|
406
456
|
private tryRefresh;
|
|
@@ -454,8 +504,16 @@ declare class HttpClient {
|
|
|
454
504
|
* caller should retry with a refreshed token. 403 is never routed here —
|
|
455
505
|
* it means "forbidden for this action" and must not trigger logout.
|
|
456
506
|
*
|
|
457
|
-
*
|
|
458
|
-
*
|
|
507
|
+
* Auth-loss ownership (#759 review):
|
|
508
|
+
* - When a refresh WAS possible (a refresh token existed), `doRefresh` owns
|
|
509
|
+
* the outcome: it clears + emits on a server-confirmed rejection, and stays
|
|
510
|
+
* silent on a transient transport failure. We must not second-guess it, so
|
|
511
|
+
* we do NOT emit here — that avoids a double clear/emit and stops a transient
|
|
512
|
+
* blip during a 401 retry from ejecting the user.
|
|
513
|
+
* - When no refresh was possible (no refresh token), this 401 IS the confirmed
|
|
514
|
+
* loss and we own it: `notifyAuthLost` clears the dead session then emits.
|
|
515
|
+
* The single clear always runs before any listener, so a listener that
|
|
516
|
+
* re-logs-in via `setTokens()` can never have its fresh session erased.
|
|
459
517
|
*/
|
|
460
518
|
private recoverFromAuthFailure;
|
|
461
519
|
private uploadFormOnce;
|
|
@@ -1933,16 +1991,35 @@ interface SpacelrClient {
|
|
|
1933
1991
|
/** Disconnect realtime WebSocket (if connected) */
|
|
1934
1992
|
disconnect(): void;
|
|
1935
1993
|
/**
|
|
1936
|
-
* Subscribe to auth loss
|
|
1994
|
+
* Subscribe to a **server-confirmed** auth loss — a genuine, durable session
|
|
1995
|
+
* invalidation (refresh token revoked/expired, or a 401 that no refresh could
|
|
1996
|
+
* recover). Fires in two situations:
|
|
1937
1997
|
* - `'refresh-failed'` — a refresh call was rejected by the server
|
|
1938
1998
|
* (refresh token revoked or expired). Note: this can also fire from a
|
|
1939
1999
|
* *background* refresh triggered inside the expiry buffer window, at
|
|
1940
2000
|
* which point the current access token may still be valid for up to
|
|
1941
2001
|
* `refreshBufferSeconds` (default 60). Treat the session as over anyway;
|
|
1942
2002
|
* the refresh token is dead.
|
|
1943
|
-
* - `'unauthorized'` — an authenticated request came back 401 and
|
|
1944
|
-
*
|
|
1945
|
-
* a successful refresh was itself 401
|
|
2003
|
+
* - `'unauthorized'` — an authenticated request came back 401 and the
|
|
2004
|
+
* session genuinely cannot be recovered: there was no refresh token to
|
|
2005
|
+
* try, or the retry after a successful refresh was itself 401. A 401 whose
|
|
2006
|
+
* refresh merely failed *transiently* (network) does NOT fire this — see
|
|
2007
|
+
* below.
|
|
2008
|
+
*
|
|
2009
|
+
* A **transient refresh failure does NOT fire this** — the SDK keeps the
|
|
2010
|
+
* session and the next request retries. That covers a network/transport
|
|
2011
|
+
* failure (offline, tunnel, captive portal, unreachable gateway; #759) AND a
|
|
2012
|
+
* **5xx** server response, e.g. a `503` the gateway returns while
|
|
2013
|
+
* oauth-service is momentarily down (#760/#762). This lets a local-first app
|
|
2014
|
+
* stay signed in through an outage instead of ejecting the user. Only a
|
|
2015
|
+
* server-confirmed rejection (a 401/invalid_grant, or another non-5xx server
|
|
2016
|
+
* error) fires.
|
|
2017
|
+
*
|
|
2018
|
+
* On a confirmed loss the SDK **clears the stored tokens itself**, so
|
|
2019
|
+
* `hasStoredSession()` reads `false` afterwards and a reload lands on the
|
|
2020
|
+
* login screen — you no longer need to call `clearTokens()` from the handler.
|
|
2021
|
+
* A `setTokens()` issued while that wipe is in flight (e.g. an external
|
|
2022
|
+
* re-login racing the loss) is applied **after** the wipe and is never lost.
|
|
1946
2023
|
*
|
|
1947
2024
|
* Typical consumer reaction: clear UI state and show the login screen.
|
|
1948
2025
|
* The event is deduped until `setTokens`/`clearTokens` is called again.
|
package/dist/index.d.ts
CHANGED
|
@@ -372,6 +372,12 @@ interface UploadProgress {
|
|
|
372
372
|
}
|
|
373
373
|
|
|
374
374
|
type RefreshCallback = (refreshToken: string) => Promise<StoredTokens>;
|
|
375
|
+
/**
|
|
376
|
+
* Reasons the SDK reports a **server-confirmed** auth loss via `onAuthLost`.
|
|
377
|
+
* A transient refresh failure (network/transport OR a 5xx) is NOT reported
|
|
378
|
+
* (#759/#762).
|
|
379
|
+
* See the `onAuthLost` JSDoc in `client.ts` for the full contract.
|
|
380
|
+
*/
|
|
375
381
|
type AuthLostReason = 'refresh-failed' | 'unauthorized';
|
|
376
382
|
type TokenRefreshedListener = (tokens: StoredTokens) => void;
|
|
377
383
|
type AuthLostListener = (reason: AuthLostReason) => void;
|
|
@@ -384,6 +390,7 @@ declare class TokenManager {
|
|
|
384
390
|
private tokenRefreshedListeners;
|
|
385
391
|
private authLostListeners;
|
|
386
392
|
private authLostEmitted;
|
|
393
|
+
private pendingClear;
|
|
387
394
|
constructor(storage?: TokenStorage, refreshBufferSeconds?: number, refreshLockName?: string);
|
|
388
395
|
setRefreshCallback(callback: RefreshCallback): void;
|
|
389
396
|
getAccessToken(): Promise<string | null>;
|
|
@@ -398,9 +405,52 @@ declare class TokenManager {
|
|
|
398
405
|
* rather than an exception that would get wrapped as a network error.
|
|
399
406
|
*/
|
|
400
407
|
forceRefresh(): Promise<string | null>;
|
|
408
|
+
/**
|
|
409
|
+
* Whether the stored session currently carries a refresh token. Used by the
|
|
410
|
+
* 401-recovery path to decide whether a refresh was even possible, so it can
|
|
411
|
+
* let `doRefresh` own the auth-loss classification instead of second-guessing
|
|
412
|
+
* it. A rejecting storage backend reads as "no refresh token".
|
|
413
|
+
*/
|
|
414
|
+
hasRefreshToken(): Promise<boolean>;
|
|
401
415
|
onTokenRefreshed(listener: TokenRefreshedListener): () => void;
|
|
402
416
|
onAuthLost(listener: AuthLostListener): () => void;
|
|
403
417
|
emitAuthLost(reason: AuthLostReason): void;
|
|
418
|
+
private dispatchAuthLost;
|
|
419
|
+
/**
|
|
420
|
+
* Handle a server-confirmed auth loss: drop the dead session, THEN notify.
|
|
421
|
+
*
|
|
422
|
+
* The stored tokens are cleared **before** listeners run (#759) so that a
|
|
423
|
+
* reload lands on the login screen and `hasStoredSession()` reads false — and,
|
|
424
|
+
* critically, so a listener that re-logs-in via `setTokens()` (synchronously
|
|
425
|
+
* OR after an await) writes its new session strictly AFTER the clear and can
|
|
426
|
+
* never be erased by it. The clear is a storage-only wipe: we do NOT reset
|
|
427
|
+
* `authLostEmitted` (as `clearTokens()` would), so the emit stays deduped for
|
|
428
|
+
* this lost session. A rejecting storage backend is swallowed — best-effort,
|
|
429
|
+
* the consumer is routed to logout regardless. `refreshPromise` is left to
|
|
430
|
+
* `doRefresh`'s own `finally`; nulling it here could clobber a newer in-flight
|
|
431
|
+
* refresh.
|
|
432
|
+
*
|
|
433
|
+
* The token-clearing auth-loss path. Called from `doRefresh` (single-flighted
|
|
434
|
+
* via `refreshPromise`) on a server-confirmed refresh rejection, and from
|
|
435
|
+
* `HttpClient` only when a 401 could not be refreshed AND no refresh was even
|
|
436
|
+
* attempted (no refresh token) — these are mutually exclusive, so there is
|
|
437
|
+
* exactly one clear per loss and it runs BEFORE any listener. A re-login a
|
|
438
|
+
* listener performs is therefore never erased.
|
|
439
|
+
*/
|
|
440
|
+
notifyAuthLost(reason: AuthLostReason): Promise<void>;
|
|
441
|
+
/**
|
|
442
|
+
* A refresh failure that is transient (retryable), not a server-confirmed auth
|
|
443
|
+
* loss — so it must NOT eject a local-first app; the next request retries.
|
|
444
|
+
* Covers:
|
|
445
|
+
* - a transport failure (offline, tunnel, captive portal, unreachable
|
|
446
|
+
* gateway) surfaced as `SpacelrNetworkError`/`SpacelrTimeoutError` (#759);
|
|
447
|
+
* - a 5xx server response — e.g. a 503 the gateway returns when oauth-service
|
|
448
|
+
* is momentarily down (#760/#762) — which is retryable infrastructure, not
|
|
449
|
+
* a token rejection.
|
|
450
|
+
* A real auth rejection (401/invalid_grant) or any other non-5xx server error
|
|
451
|
+
* is NOT transient and is treated as a server-confirmed loss.
|
|
452
|
+
*/
|
|
453
|
+
private isTransientTransportError;
|
|
404
454
|
private isTokenExpired;
|
|
405
455
|
private shouldRefresh;
|
|
406
456
|
private tryRefresh;
|
|
@@ -454,8 +504,16 @@ declare class HttpClient {
|
|
|
454
504
|
* caller should retry with a refreshed token. 403 is never routed here —
|
|
455
505
|
* it means "forbidden for this action" and must not trigger logout.
|
|
456
506
|
*
|
|
457
|
-
*
|
|
458
|
-
*
|
|
507
|
+
* Auth-loss ownership (#759 review):
|
|
508
|
+
* - When a refresh WAS possible (a refresh token existed), `doRefresh` owns
|
|
509
|
+
* the outcome: it clears + emits on a server-confirmed rejection, and stays
|
|
510
|
+
* silent on a transient transport failure. We must not second-guess it, so
|
|
511
|
+
* we do NOT emit here — that avoids a double clear/emit and stops a transient
|
|
512
|
+
* blip during a 401 retry from ejecting the user.
|
|
513
|
+
* - When no refresh was possible (no refresh token), this 401 IS the confirmed
|
|
514
|
+
* loss and we own it: `notifyAuthLost` clears the dead session then emits.
|
|
515
|
+
* The single clear always runs before any listener, so a listener that
|
|
516
|
+
* re-logs-in via `setTokens()` can never have its fresh session erased.
|
|
459
517
|
*/
|
|
460
518
|
private recoverFromAuthFailure;
|
|
461
519
|
private uploadFormOnce;
|
|
@@ -1933,16 +1991,35 @@ interface SpacelrClient {
|
|
|
1933
1991
|
/** Disconnect realtime WebSocket (if connected) */
|
|
1934
1992
|
disconnect(): void;
|
|
1935
1993
|
/**
|
|
1936
|
-
* Subscribe to auth loss
|
|
1994
|
+
* Subscribe to a **server-confirmed** auth loss — a genuine, durable session
|
|
1995
|
+
* invalidation (refresh token revoked/expired, or a 401 that no refresh could
|
|
1996
|
+
* recover). Fires in two situations:
|
|
1937
1997
|
* - `'refresh-failed'` — a refresh call was rejected by the server
|
|
1938
1998
|
* (refresh token revoked or expired). Note: this can also fire from a
|
|
1939
1999
|
* *background* refresh triggered inside the expiry buffer window, at
|
|
1940
2000
|
* which point the current access token may still be valid for up to
|
|
1941
2001
|
* `refreshBufferSeconds` (default 60). Treat the session as over anyway;
|
|
1942
2002
|
* the refresh token is dead.
|
|
1943
|
-
* - `'unauthorized'` — an authenticated request came back 401 and
|
|
1944
|
-
*
|
|
1945
|
-
* a successful refresh was itself 401
|
|
2003
|
+
* - `'unauthorized'` — an authenticated request came back 401 and the
|
|
2004
|
+
* session genuinely cannot be recovered: there was no refresh token to
|
|
2005
|
+
* try, or the retry after a successful refresh was itself 401. A 401 whose
|
|
2006
|
+
* refresh merely failed *transiently* (network) does NOT fire this — see
|
|
2007
|
+
* below.
|
|
2008
|
+
*
|
|
2009
|
+
* A **transient refresh failure does NOT fire this** — the SDK keeps the
|
|
2010
|
+
* session and the next request retries. That covers a network/transport
|
|
2011
|
+
* failure (offline, tunnel, captive portal, unreachable gateway; #759) AND a
|
|
2012
|
+
* **5xx** server response, e.g. a `503` the gateway returns while
|
|
2013
|
+
* oauth-service is momentarily down (#760/#762). This lets a local-first app
|
|
2014
|
+
* stay signed in through an outage instead of ejecting the user. Only a
|
|
2015
|
+
* server-confirmed rejection (a 401/invalid_grant, or another non-5xx server
|
|
2016
|
+
* error) fires.
|
|
2017
|
+
*
|
|
2018
|
+
* On a confirmed loss the SDK **clears the stored tokens itself**, so
|
|
2019
|
+
* `hasStoredSession()` reads `false` afterwards and a reload lands on the
|
|
2020
|
+
* login screen — you no longer need to call `clearTokens()` from the handler.
|
|
2021
|
+
* A `setTokens()` issued while that wipe is in flight (e.g. an external
|
|
2022
|
+
* re-login racing the loss) is applied **after** the wipe and is never lost.
|
|
1946
2023
|
*
|
|
1947
2024
|
* Typical consumer reaction: clear UI state and show the login screen.
|
|
1948
2025
|
* The event is deduped until `setTokens`/`clearTokens` is called again.
|
package/dist/index.js
CHANGED
|
@@ -215,17 +215,27 @@ var HttpClient = class {
|
|
|
215
215
|
* caller should retry with a refreshed token. 403 is never routed here —
|
|
216
216
|
* it means "forbidden for this action" and must not trigger logout.
|
|
217
217
|
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
218
|
+
* Auth-loss ownership (#759 review):
|
|
219
|
+
* - When a refresh WAS possible (a refresh token existed), `doRefresh` owns
|
|
220
|
+
* the outcome: it clears + emits on a server-confirmed rejection, and stays
|
|
221
|
+
* silent on a transient transport failure. We must not second-guess it, so
|
|
222
|
+
* we do NOT emit here — that avoids a double clear/emit and stops a transient
|
|
223
|
+
* blip during a 401 retry from ejecting the user.
|
|
224
|
+
* - When no refresh was possible (no refresh token), this 401 IS the confirmed
|
|
225
|
+
* loss and we own it: `notifyAuthLost` clears the dead session then emits.
|
|
226
|
+
* The single clear always runs before any listener, so a listener that
|
|
227
|
+
* re-logs-in via `setTokens()` can never have its fresh session erased.
|
|
220
228
|
*/
|
|
221
229
|
async recoverFromAuthFailure(isRetry) {
|
|
222
230
|
if (isRetry) {
|
|
223
|
-
this.tokenManager.
|
|
231
|
+
await this.tokenManager.notifyAuthLost("unauthorized");
|
|
224
232
|
return false;
|
|
225
233
|
}
|
|
234
|
+
const hadRefreshToken = await this.tokenManager.hasRefreshToken();
|
|
226
235
|
const refreshed = await this.tokenManager.forceRefresh();
|
|
227
236
|
if (refreshed) return true;
|
|
228
|
-
|
|
237
|
+
if (hadRefreshToken) return false;
|
|
238
|
+
await this.tokenManager.notifyAuthLost("unauthorized");
|
|
229
239
|
return false;
|
|
230
240
|
}
|
|
231
241
|
async uploadFormOnce(url, headers, formData, timeoutMs) {
|
|
@@ -457,6 +467,11 @@ var TokenManager = class {
|
|
|
457
467
|
// that hits `/auth/logout` with a dead token) don't re-emit and loop.
|
|
458
468
|
// Cleared by setTokens() / clearTokens() to re-arm for the next session.
|
|
459
469
|
this.authLostEmitted = false;
|
|
470
|
+
// The in-flight auth-loss storage wipe, if any. `setTokens` awaits it before
|
|
471
|
+
// writing so a re-login that lands DURING the wipe (e.g. an external
|
|
472
|
+
// auth-components bridge calling setTokens concurrently with a background
|
|
473
|
+
// refresh failure) is applied AFTER the wipe and never erased (#759 review).
|
|
474
|
+
this.pendingClear = null;
|
|
460
475
|
this.storage = storage ?? new MemoryTokenStorage();
|
|
461
476
|
this.refreshBufferSeconds = refreshBufferSeconds;
|
|
462
477
|
this.refreshLockName = refreshLockName;
|
|
@@ -478,6 +493,7 @@ var TokenManager = class {
|
|
|
478
493
|
return tokens.accessToken;
|
|
479
494
|
}
|
|
480
495
|
async setTokens(tokens) {
|
|
496
|
+
if (this.pendingClear) await this.pendingClear;
|
|
481
497
|
const needsDefault = tokens.expiresAt === void 0 && !!tokens.refreshToken;
|
|
482
498
|
const normalised = needsDefault ? { ...tokens, expiresAt: Math.floor(Date.now() / 1e3) } : tokens;
|
|
483
499
|
await this.storage.setTokens(normalised);
|
|
@@ -510,6 +526,16 @@ var TokenManager = class {
|
|
|
510
526
|
const refreshed = await this.tryRefresh(tokens);
|
|
511
527
|
return refreshed?.accessToken ?? null;
|
|
512
528
|
}
|
|
529
|
+
/**
|
|
530
|
+
* Whether the stored session currently carries a refresh token. Used by the
|
|
531
|
+
* 401-recovery path to decide whether a refresh was even possible, so it can
|
|
532
|
+
* let `doRefresh` own the auth-loss classification instead of second-guessing
|
|
533
|
+
* it. A rejecting storage backend reads as "no refresh token".
|
|
534
|
+
*/
|
|
535
|
+
async hasRefreshToken() {
|
|
536
|
+
const tokens = await this.safeGetTokens();
|
|
537
|
+
return !!tokens?.refreshToken;
|
|
538
|
+
}
|
|
513
539
|
onTokenRefreshed(listener) {
|
|
514
540
|
this.tokenRefreshedListeners.add(listener);
|
|
515
541
|
return () => this.tokenRefreshedListeners.delete(listener);
|
|
@@ -521,6 +547,9 @@ var TokenManager = class {
|
|
|
521
547
|
emitAuthLost(reason) {
|
|
522
548
|
if (this.authLostEmitted) return;
|
|
523
549
|
this.authLostEmitted = true;
|
|
550
|
+
this.dispatchAuthLost(reason);
|
|
551
|
+
}
|
|
552
|
+
dispatchAuthLost(reason) {
|
|
524
553
|
for (const listener of this.authLostListeners) {
|
|
525
554
|
try {
|
|
526
555
|
listener(reason);
|
|
@@ -528,6 +557,57 @@ var TokenManager = class {
|
|
|
528
557
|
}
|
|
529
558
|
}
|
|
530
559
|
}
|
|
560
|
+
/**
|
|
561
|
+
* Handle a server-confirmed auth loss: drop the dead session, THEN notify.
|
|
562
|
+
*
|
|
563
|
+
* The stored tokens are cleared **before** listeners run (#759) so that a
|
|
564
|
+
* reload lands on the login screen and `hasStoredSession()` reads false — and,
|
|
565
|
+
* critically, so a listener that re-logs-in via `setTokens()` (synchronously
|
|
566
|
+
* OR after an await) writes its new session strictly AFTER the clear and can
|
|
567
|
+
* never be erased by it. The clear is a storage-only wipe: we do NOT reset
|
|
568
|
+
* `authLostEmitted` (as `clearTokens()` would), so the emit stays deduped for
|
|
569
|
+
* this lost session. A rejecting storage backend is swallowed — best-effort,
|
|
570
|
+
* the consumer is routed to logout regardless. `refreshPromise` is left to
|
|
571
|
+
* `doRefresh`'s own `finally`; nulling it here could clobber a newer in-flight
|
|
572
|
+
* refresh.
|
|
573
|
+
*
|
|
574
|
+
* The token-clearing auth-loss path. Called from `doRefresh` (single-flighted
|
|
575
|
+
* via `refreshPromise`) on a server-confirmed refresh rejection, and from
|
|
576
|
+
* `HttpClient` only when a 401 could not be refreshed AND no refresh was even
|
|
577
|
+
* attempted (no refresh token) — these are mutually exclusive, so there is
|
|
578
|
+
* exactly one clear per loss and it runs BEFORE any listener. A re-login a
|
|
579
|
+
* listener performs is therefore never erased.
|
|
580
|
+
*/
|
|
581
|
+
async notifyAuthLost(reason) {
|
|
582
|
+
if (this.authLostEmitted) return;
|
|
583
|
+
this.authLostEmitted = true;
|
|
584
|
+
this.pendingClear = this.storage.clearTokens().catch(() => {
|
|
585
|
+
});
|
|
586
|
+
await this.pendingClear;
|
|
587
|
+
this.pendingClear = null;
|
|
588
|
+
this.dispatchAuthLost(reason);
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* A refresh failure that is transient (retryable), not a server-confirmed auth
|
|
592
|
+
* loss — so it must NOT eject a local-first app; the next request retries.
|
|
593
|
+
* Covers:
|
|
594
|
+
* - a transport failure (offline, tunnel, captive portal, unreachable
|
|
595
|
+
* gateway) surfaced as `SpacelrNetworkError`/`SpacelrTimeoutError` (#759);
|
|
596
|
+
* - a 5xx server response — e.g. a 503 the gateway returns when oauth-service
|
|
597
|
+
* is momentarily down (#760/#762) — which is retryable infrastructure, not
|
|
598
|
+
* a token rejection.
|
|
599
|
+
* A real auth rejection (401/invalid_grant) or any other non-5xx server error
|
|
600
|
+
* is NOT transient and is treated as a server-confirmed loss.
|
|
601
|
+
*/
|
|
602
|
+
isTransientTransportError(error) {
|
|
603
|
+
if (error instanceof SpacelrNetworkError || error instanceof SpacelrTimeoutError) {
|
|
604
|
+
return true;
|
|
605
|
+
}
|
|
606
|
+
if (error instanceof SpacelrError && typeof error.statusCode === "number") {
|
|
607
|
+
return error.statusCode >= 500 && error.statusCode <= 599;
|
|
608
|
+
}
|
|
609
|
+
return false;
|
|
610
|
+
}
|
|
531
611
|
isTokenExpired(tokens) {
|
|
532
612
|
if (!tokens.expiresAt) return false;
|
|
533
613
|
return Date.now() >= tokens.expiresAt * 1e3;
|
|
@@ -566,15 +646,18 @@ var TokenManager = class {
|
|
|
566
646
|
return current;
|
|
567
647
|
}
|
|
568
648
|
const activeRefreshToken = current?.refreshToken || refreshToken;
|
|
649
|
+
let newTokens;
|
|
569
650
|
try {
|
|
570
|
-
|
|
571
|
-
await this.storage.setTokens(newTokens);
|
|
572
|
-
this.emitTokenRefreshed(newTokens);
|
|
573
|
-
return newTokens;
|
|
651
|
+
newTokens = await callback(activeRefreshToken);
|
|
574
652
|
} catch (error) {
|
|
575
|
-
this.
|
|
653
|
+
if (!this.isTransientTransportError(error)) {
|
|
654
|
+
await this.notifyAuthLost("refresh-failed");
|
|
655
|
+
}
|
|
576
656
|
throw error;
|
|
577
657
|
}
|
|
658
|
+
await this.storage.setTokens(newTokens);
|
|
659
|
+
this.emitTokenRefreshed(newTokens);
|
|
660
|
+
return newTokens;
|
|
578
661
|
}
|
|
579
662
|
async safeGetTokens() {
|
|
580
663
|
try {
|