Skip to content

Commit 865b0ec

Browse files
committed
chore: cache effective entitlments in the main entitlments api for consumers
1 parent 51a0881 commit 865b0ec

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

src/services/entitlements.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,24 @@ define(function (require, exports, module) {
7070
});
7171
}
7272

73+
let effectiveEntitlements = null;
74+
async function _getEffectiveEntitlements() {
75+
if(effectiveEntitlements){
76+
return effectiveEntitlements;
77+
}
78+
const entitlements = await LoginService.getEffectiveEntitlements();
79+
effectiveEntitlements = entitlements;
80+
return entitlements;
81+
}
82+
7383
/**
7484
* Get the plan details from entitlements with fallback to free plan defaults. If the user is
7585
* in pro trial(isInProTrial API), then paidSubscriber will always be true as we need to treat user as paid.
7686
* you should use isInProTrial API to check if user is in pro trial if some trial-related logic needs to be done.
7787
* @returns {Promise<Object>} Plan details object
7888
*/
7989
async function getPlanDetails() {
80-
const entitlements = await LoginService.getEffectiveEntitlements();
90+
const entitlements = await _getEffectiveEntitlements();
8191

8292
if (entitlements && entitlements.plan) {
8393
return entitlements.plan;
@@ -97,7 +107,7 @@ define(function (require, exports, module) {
97107
* @returns {Promise<boolean>} True if user is in pro trial, false otherwise
98108
*/
99109
async function isInProTrial() {
100-
const entitlements = await LoginService.getEffectiveEntitlements();
110+
const entitlements = await _getEffectiveEntitlements();
101111
return !!(entitlements && entitlements.isInProTrial);
102112
}
103113

@@ -106,7 +116,7 @@ define(function (require, exports, module) {
106116
* @returns {Promise<number>} Number of remaining trial days
107117
*/
108118
async function getTrialRemainingDays() {
109-
const entitlements = await LoginService.getEffectiveEntitlements();
119+
const entitlements = await _getEffectiveEntitlements();
110120
return entitlements && entitlements.trialDaysRemaining ? entitlements.trialDaysRemaining : 0;
111121
}
112122

@@ -142,7 +152,7 @@ define(function (require, exports, module) {
142152
* }
143153
*/
144154
async function getLiveEditEntitlement() {
145-
const entitlements = await LoginService.getEffectiveEntitlements();
155+
const entitlements = await _getEffectiveEntitlements();
146156

147157
if (entitlements && entitlements.entitlements && entitlements.entitlements.liveEdit) {
148158
return entitlements.entitlements.liveEdit;
@@ -168,6 +178,7 @@ define(function (require, exports, module) {
168178
LoginService = KernalModeTrust.loginService;
169179
// Set up event forwarding from LoginService
170180
LoginService.on(LoginService.EVENT_ENTITLEMENTS_CHANGED, function() {
181+
effectiveEntitlements = null;
171182
Entitlements.trigger(EVENT_ENTITLEMENTS_CHANGED);
172183
});
173184
}

src/services/login-service.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,10 @@ define(function (require, exports, module) {
213213

214214

215215
/**
216-
* Get entitlements from API or cache
217-
* Returns null if user is not logged in
216+
* Get entitlements from API or disc cache.
217+
* @param {string} forceRefresh If provided will always fetch from server and bypass cache. Use rarely like
218+
* when a user logs in/out/some other user activity/ account-related events.
219+
* Returns null if the user is not logged in
218220
*/
219221
async function getEntitlements(forceRefresh = false) {
220222
// Return null if not logged in
@@ -413,8 +415,8 @@ define(function (require, exports, module) {
413415
}
414416

415417
/**
416-
* Get effective entitlements for determining feature availability throughout the app.
417-
* This is the primary API that should be used across Phoenix to check entitlements and enable/disable features.
418+
* Get effective entitlements for determining feature availability.
419+
* This is for internal use only. All consumers in phoenix code should use `KernalModeTrust.Entitlements` APIs.
418420
*
419421
* @returns {Promise<Object|null>} Entitlements object or null if not logged in and no trial active
420422
*

src/services/readme-login-browser-no_dist.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,22 @@ For testing with a local account server instance:
107107
- Start your local account development stack on `localhost:5000`
108108
- Ensure all login endpoints are properly configured
109109

110-
3. **Login and Copy Session:**
110+
Now just visit login server at `http://localhost:5000` and login. It should work with phoenix code dev server
111+
at https://localhost:8000/src when you run phoenix code dev server via `npm run serve`. This works without any
112+
manual cookie copy needed as the dev server sets cookies localhost wide. But if that didnt work, please see
113+
manual cookie copy instructions below.
114+
115+
1. **Login and Copy Session:**
111116
- Navigate to `http://localhost:5000` in browser
112117
- Login with test credentials
113118
- Copy `session` cookie value from DevTools
114119

115-
4. **Set Cookie in Phoenix App:**
120+
2. **Set Cookie in Phoenix App:**
116121
- Navigate to `http://localhost:8000/src/`
117122
- Open Chrome DevTools → Application → Cookies
118123
- Create `session` cookie with copied value (same process as above)
119124

120-
5. **Verify Local Integration:**
125+
3. **Verify Local Integration:**
121126
- API calls from localhost:8000 now route through serve-proxy.js to localhost:5000
122127
- Authentication should work with local account server
123128

0 commit comments

Comments
 (0)