Skip to content

Commit 9ef4ca0

Browse files
authored
fix: trigger access token refresh (#7149)
## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> - Previously `triggerAccessTokenRefresh` was only called during `getSubscriptions` polling, normal subscription state change didn't trigger it. This PR trigger `triggerAccessTokenRefresh` everytime subscription state change - Remove `triggerAccessTokenRefresh` from `startShieldSubscriptionWithCard` since that only return stripe checkout url, not subscribed yet. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Refresh access token immediately when subscription state changes and remove refresh after starting card/crypto checkout; delete polling flag/logic. > > - **Subscription Controller**: > - **Behavior**: Call `triggerAccessTokenRefresh` in `getSubscriptions` whenever state changes (subscriptions/customer/trials/lastSubscription). > - **Refactor**: Remove `#shouldCallRefreshAuthToken` and related polling conditional; `_executePoll` now only calls `getSubscriptions`. > - **Flows**: Remove `triggerAccessTokenRefresh` after `startShieldSubscriptionWithCard` and `startSubscriptionWithCrypto` (checkout/session creation only). > - **Docs**: > - Update `CHANGELOG.md` to reflect the new refresh behavior and removal after card checkout. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 04b3ce9. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 9629601 commit 9ef4ca0

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

packages/subscription-controller/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Trigger `triggerAccessTokenRefresh` everytime subscription state change instead of only when polling ([#7149](https://github.com/MetaMask/core/pull/7149))
13+
- Remove `triggerAccessTokenRefresh` after `startShieldSubscriptionWithCard` ([#7149](https://github.com/MetaMask/core/pull/7149))
14+
1015
## [4.2.1]
1116

1217
### Added

packages/subscription-controller/src/SubscriptionController.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,6 @@ export class SubscriptionController extends StaticIntervalPollingController()<
241241
> {
242242
readonly #subscriptionService: ISubscriptionService;
243243

244-
#shouldCallRefreshAuthToken: boolean = false;
245-
246244
/**
247245
* Creates a new SubscriptionController instance.
248246
*
@@ -391,7 +389,8 @@ export class SubscriptionController extends StaticIntervalPollingController()<
391389
state.trialedProducts = newTrialedProducts;
392390
state.lastSubscription = newLastSubscription;
393391
});
394-
this.#shouldCallRefreshAuthToken = true;
392+
// trigger access token refresh to ensure the user has the latest access token if subscription state change
393+
this.triggerAccessTokenRefresh();
395394
}
396395

397396
return newSubscriptions;
@@ -466,8 +465,7 @@ export class SubscriptionController extends StaticIntervalPollingController()<
466465

467466
const response =
468467
await this.#subscriptionService.startSubscriptionWithCard(request);
469-
470-
this.triggerAccessTokenRefresh();
468+
// note: no need to trigger access token refresh after startSubscriptionWithCard request because this only return stripe checkout session url, subscription not created yet
471469

472470
return response;
473471
}
@@ -476,7 +474,7 @@ export class SubscriptionController extends StaticIntervalPollingController()<
476474
this.#assertIsUserNotSubscribed({ products: request.products });
477475
const response =
478476
await this.#subscriptionService.startSubscriptionWithCrypto(request);
479-
this.triggerAccessTokenRefresh();
477+
480478
return response;
481479
}
482480

@@ -730,10 +728,6 @@ export class SubscriptionController extends StaticIntervalPollingController()<
730728

731729
async _executePoll(): Promise<void> {
732730
await this.getSubscriptions();
733-
if (this.#shouldCallRefreshAuthToken) {
734-
this.triggerAccessTokenRefresh();
735-
this.#shouldCallRefreshAuthToken = false;
736-
}
737731
}
738732

739733
/**

0 commit comments

Comments
 (0)