@@ -2,7 +2,8 @@ define(function (require, exports, module) {
22 const Mustache = require ( "thirdparty/mustache/mustache" ) ,
33 PopUpManager = require ( "widgets/PopUpManager" ) ,
44 ThemeManager = require ( "view/ThemeManager" ) ,
5- Strings = require ( "strings" ) ;
5+ Strings = require ( "strings" ) ,
6+ LoginService = require ( "./login-service" ) ;
67
78 const KernalModeTrust = window . KernalModeTrust ;
89 if ( ! KernalModeTrust ) {
@@ -183,74 +184,6 @@ define(function (require, exports, module) {
183184 _setupDocumentClickHandler ( ) ;
184185 }
185186
186- // Cached entitlements data
187- let cachedEntitlements = null ;
188-
189- /**
190- * Get entitlements from API or cache
191- * Returns null if user is not logged in
192- */
193- async function getEntitlements ( forceRefresh = false ) {
194- // Return null if not logged in
195- if ( ! KernalModeTrust . loginService . isLoggedIn ( ) ) {
196- return null ;
197- }
198-
199- // Return cached data if available and not forcing refresh
200- if ( cachedEntitlements && ! forceRefresh ) {
201- return cachedEntitlements ;
202- }
203-
204- try {
205- const accountBaseURL = KernalModeTrust . loginService . getAccountBaseURL ( ) ;
206- const language = Phoenix . app && Phoenix . app . language ? Phoenix . app . language : 'en' ;
207- let url = `${ accountBaseURL } /getAppEntitlements?lang=${ language } ` ;
208- let fetchOptions = {
209- method : 'GET' ,
210- headers : {
211- 'Accept' : 'application/json'
212- }
213- } ;
214-
215- // Handle different authentication methods for browser vs desktop
216- if ( Phoenix . isNativeApp ) {
217- // Desktop app: use appSessionID and validationCode
218- const profile = KernalModeTrust . loginService . getProfile ( ) ;
219- if ( profile && profile . apiKey && profile . validationCode ) {
220- url += `&appSessionID=${ encodeURIComponent ( profile . apiKey ) } &validationCode=${ encodeURIComponent ( profile . validationCode ) } ` ;
221- } else {
222- console . error ( 'Missing appSessionID or validationCode for desktop app entitlements' ) ;
223- return null ;
224- }
225- } else {
226- // Browser app: use session cookies
227- fetchOptions . credentials = 'include' ;
228- }
229-
230- const response = await fetch ( url , fetchOptions ) ;
231-
232- if ( response . ok ) {
233- const result = await response . json ( ) ;
234- if ( result . isSuccess ) {
235- // Check if entitlements actually changed
236- const entitlementsChanged = JSON . stringify ( cachedEntitlements ) !== JSON . stringify ( result ) ;
237-
238- cachedEntitlements = result ;
239-
240- // Trigger event if entitlements changed
241- if ( entitlementsChanged && KernalModeTrust . loginService . trigger ) {
242- KernalModeTrust . loginService . trigger ( KernalModeTrust . loginService . EVENT_ENTITLEMENTS_CHANGED , result ) ;
243- }
244-
245- return cachedEntitlements ;
246- }
247- }
248- } catch ( error ) {
249- console . error ( 'Failed to fetch entitlements:' , error ) ;
250- }
251-
252- return null ;
253- }
254187
255188 let userEmail = "" ;
256189 class SecureEmail extends HTMLElement {
@@ -422,11 +355,8 @@ define(function (require, exports, module) {
422355 Strings : Strings
423356 } ;
424357
425- // Check for cached entitlements synchronously for immediate basic plan info
426- if ( cachedEntitlements && cachedEntitlements . plan ) {
427- templateData . planClass = cachedEntitlements . plan . paidSubscriber ? "user-plan-paid" : "user-plan-free" ;
428- templateData . planName = cachedEntitlements . plan . name ;
429- }
358+ // Note: We don't await here to keep popup display instant
359+ // Cached entitlements will be applied asynchronously after popup is shown
430360
431361 // Render template with data immediately
432362 const renderedTemplate = Mustache . render ( profileTemplate , templateData ) ;
@@ -435,11 +365,16 @@ define(function (require, exports, module) {
435365 $ ( "body" ) . append ( $popup ) ;
436366 isPopupVisible = true ;
437367
438- // Apply cached entitlements immediately if available (including quota/messages)
439- if ( cachedEntitlements ) {
440- _updatePopupWithEntitlements ( cachedEntitlements ) ;
441- }
442368 positionPopup ( ) ;
369+
370+ // Apply cached entitlements immediately if available (including quota/messages)
371+ KernalModeTrust . loginService . getEntitlements ( false ) . then ( cachedEntitlements => {
372+ if ( cachedEntitlements && isPopupVisible ) {
373+ _updatePopupWithEntitlements ( cachedEntitlements ) ;
374+ }
375+ } ) . catch ( error => {
376+ console . error ( 'Failed to apply cached entitlements to popup:' , error ) ;
377+ } ) ;
443378
444379 PopUpManager . addPopUp ( $popup , function ( ) {
445380 $popup . remove ( ) ;
@@ -483,7 +418,7 @@ define(function (require, exports, module) {
483418 async function _refreshEntitlementsInBackground ( ) {
484419 try {
485420 // Fetch fresh entitlements from API
486- const freshEntitlements = await getEntitlements ( true ) ; // Force refresh to get latest data
421+ const freshEntitlements = await KernalModeTrust . loginService . getEntitlements ( true ) ; // Force refresh to get latest data
487422
488423 // Only update popup if it's still visible
489424 if ( isPopupVisible && $popup && freshEntitlements ) {
@@ -568,14 +503,7 @@ define(function (require, exports, module) {
568503 _removeProfileIcon ( ) ;
569504
570505 // Clear cached entitlements when user logs out
571- if ( cachedEntitlements ) {
572- cachedEntitlements = null ;
573-
574- // Trigger event when entitlements are cleared
575- if ( KernalModeTrust . loginService . trigger ) {
576- KernalModeTrust . loginService . trigger ( KernalModeTrust . loginService . EVENT_ENTITLEMENTS_CHANGED , null ) ;
577- }
578- }
506+ LoginService . clearEntitlements ( ) ;
579507 }
580508
581509 function setLoggedIn ( initial , color ) {
@@ -586,13 +514,12 @@ define(function (require, exports, module) {
586514 _updateProfileIcon ( initial , color ) ;
587515
588516 // Preload entitlements when user logs in
589- getEntitlements ( ) . catch ( error => {
517+ KernalModeTrust . loginService . getEntitlements ( ) . catch ( error => {
590518 console . error ( 'Failed to preload entitlements on login:' , error ) ;
591519 } ) ;
592520 }
593521
594522 exports . init = init ;
595523 exports . setNotLoggedIn = setNotLoggedIn ;
596524 exports . setLoggedIn = setLoggedIn ;
597- exports . getEntitlements = getEntitlements ;
598525} ) ;
0 commit comments