astro-tokenkit 1.0.19 → 1.0.21

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.
@@ -64,7 +64,7 @@ export function tokenKit(config) {
64
64
  }
65
65
  // Always inject the client-side script for idle monitoring
66
66
  injectScript('page', `import 'astro-tokenkit/client-init';`);
67
- logger.debug('[TokenKit] Integration initialized');
67
+ logger.debug('[TokenKit] Integration initialized', !!config.debug);
68
68
  },
69
69
  },
70
70
  };
@@ -412,13 +412,13 @@ function safeFetch(url, init, config) {
412
412
  * Logger utility that respects the debug flag in the configuration
413
413
  */
414
414
  const logger = {
415
- debug: (message, ...args) => {
416
- if (getConfig().debug) {
415
+ debug: (message, force, ...args) => {
416
+ if (force || getConfig().debug) {
417
417
  console.debug(message, ...args);
418
418
  }
419
419
  },
420
- info: (message, ...args) => {
421
- if (getConfig().debug) {
420
+ info: (message, force, ...args) => {
421
+ if (force || getConfig().debug) {
422
422
  console.log(message, ...args);
423
423
  }
424
424
  },
@@ -544,11 +544,27 @@ class TokenManager {
544
544
  */
545
545
  refresh(ctx, refreshToken, options, headers) {
546
546
  return __awaiter(this, void 0, void 0, function* () {
547
+ logger.debug('[TokenKit] Starting token refresh', !!this.config.debug);
547
548
  try {
548
- return yield this.performRefresh(ctx, refreshToken, options, headers);
549
+ const bundle = yield this.performRefresh(ctx, refreshToken, options, headers);
550
+ if (bundle) {
551
+ if (this.config.onRefresh) {
552
+ yield this.config.onRefresh(bundle, ctx);
553
+ }
554
+ }
555
+ else {
556
+ logger.debug('[TokenKit] Token refresh returned no bundle (invalid or expired)', !!this.config.debug);
557
+ if (this.config.onRefreshError) {
558
+ yield this.config.onRefreshError(new AuthError('Refresh token invalid or expired', 401), ctx);
559
+ }
560
+ }
561
+ return bundle;
549
562
  }
550
563
  catch (error) {
551
- clearTokens(ctx, this.config.cookies);
564
+ logger.debug(`[TokenKit] Token refresh failed: ${error.message}`, !!this.config.debug);
565
+ if (this.config.onRefreshError) {
566
+ yield this.config.onRefreshError(error, ctx);
567
+ }
552
568
  throw error;
553
569
  }
554
570
  });
@@ -627,14 +643,19 @@ class TokenManager {
627
643
  const tokens = retrieveTokens(ctx, this.config.cookies);
628
644
  // No tokens
629
645
  if (!tokens.accessToken || !tokens.refreshToken || !tokens.expiresAt) {
646
+ logger.debug('[TokenKit] No valid session found, refresh impossible', !!this.config.debug);
630
647
  return null;
631
648
  }
632
649
  // Token expired or force refresh
633
- if (force || isExpired(tokens.expiresAt, now, this.config.policy)) {
650
+ const expired = isExpired(tokens.expiresAt, now, this.config.policy);
651
+ if (force || expired) {
652
+ logger.debug(`[TokenKit] Token ${force ? 'force refresh' : 'expired'}, refreshing...`, !!this.config.debug);
634
653
  const flightKey = this.createFlightKey(tokens.refreshToken);
635
654
  const bundle = yield this.singleFlight.execute(flightKey, () => this.refresh(ctx, tokens.refreshToken, options, headers));
636
- if (!bundle)
655
+ if (!bundle) {
656
+ logger.debug('[TokenKit] Refresh returned no bundle, session lost', !!this.config.debug);
637
657
  return null;
658
+ }
638
659
  // Ensure tokens are stored in the current context (in case of shared flight)
639
660
  storeTokens(ctx, bundle, this.config.cookies);
640
661
  return {
@@ -646,19 +667,26 @@ class TokenManager {
646
667
  }
647
668
  // Proactive refresh
648
669
  if (shouldRefresh(tokens.expiresAt, now, tokens.lastRefreshAt, this.config.policy)) {
670
+ logger.debug('[TokenKit] Token near expiration, performing proactive refresh', !!this.config.debug);
649
671
  const flightKey = this.createFlightKey(tokens.refreshToken);
650
- const bundle = yield this.singleFlight.execute(flightKey, () => this.refresh(ctx, tokens.refreshToken, options, headers));
651
- if (bundle) {
652
- // Ensure tokens are stored in the current context (in case of shared flight)
653
- storeTokens(ctx, bundle, this.config.cookies);
654
- return {
655
- accessToken: bundle.accessToken,
656
- expiresAt: bundle.accessExpiresAt,
657
- tokenType: bundle.tokenType,
658
- payload: (_d = (_c = bundle.sessionPayload) !== null && _c !== void 0 ? _c : parseJWTPayload(bundle.accessToken)) !== null && _d !== void 0 ? _d : undefined,
659
- };
672
+ try {
673
+ const bundle = yield this.singleFlight.execute(flightKey, () => this.refresh(ctx, tokens.refreshToken, options, headers));
674
+ if (bundle) {
675
+ logger.debug('[TokenKit] Proactive refresh successful', !!this.config.debug);
676
+ // Ensure tokens are stored in the current context (in case of shared flight)
677
+ storeTokens(ctx, bundle, this.config.cookies);
678
+ return {
679
+ accessToken: bundle.accessToken,
680
+ expiresAt: bundle.accessExpiresAt,
681
+ tokenType: bundle.tokenType,
682
+ payload: (_d = (_c = bundle.sessionPayload) !== null && _c !== void 0 ? _c : parseJWTPayload(bundle.accessToken)) !== null && _d !== void 0 ? _d : undefined,
683
+ };
684
+ }
685
+ }
686
+ catch (error) {
687
+ logger.debug(`[TokenKit] Proactive refresh failed: ${error.message}. Continuing with current token.`, !!this.config.debug);
660
688
  }
661
- // Refresh failed, check if tokens still exist
689
+ // Refresh failed or returned no bundle, check if tokens still exist
662
690
  const currentTokens = retrieveTokens(ctx, this.config.cookies);
663
691
  if (!currentTokens.accessToken) {
664
692
  return null;
@@ -700,7 +728,7 @@ class TokenManager {
700
728
  }
701
729
  catch (error) {
702
730
  // Ignore logout endpoint errors
703
- logger.debug('[TokenKit] Logout endpoint failed:', error);
731
+ logger.debug('[TokenKit] Logout endpoint failed:', !!this.config.debug, error);
704
732
  }
705
733
  finally {
706
734
  clearTimeout(timeoutId);
@@ -848,7 +876,7 @@ function createMiddleware() {
848
876
  else if (config.context) {
849
877
  contextStrategy = 'custom (external AsyncLocalStorage)';
850
878
  }
851
- logger.debug(`[TokenKit] Middleware initialized (auth: ${authStatus}, context: ${contextStrategy})`);
879
+ logger.debug(`[TokenKit] Middleware initialized (auth: ${authStatus}, context: ${contextStrategy})`, !!config.debug);
852
880
  globalStorage[LOGGED_KEY] = true;
853
881
  }
854
882
  const runLogic = () => __awaiter(this, void 0, void 0, function* () {
@@ -860,7 +888,7 @@ function createMiddleware() {
860
888
  }
861
889
  catch (error) {
862
890
  // Log only the message to avoid leaking sensitive data in the error object
863
- logger.debug('[TokenKit] Automatic token rotation failed:', error.message || error);
891
+ logger.debug('[TokenKit] Automatic token rotation failed:', !!config.debug, error.message || error);
864
892
  }
865
893
  }
866
894
  return next();