@@ -12,7 +12,11 @@ class LicenseManager {
1212 private STORAGE_KEY_TYPE = 'easyeditor-user-type' ;
1313 private STORAGE_KEY_LICENSE_KEY = 'easyeditor-user-license-key' ;
1414
15- private constructor ( ) { }
15+ private constructor ( ) {
16+ // Immediately restore cached license state so components get the right
17+ // value on their very first render (before the async API check completes).
18+ this . restoreFromCache ( ) ;
19+ }
1620
1721 public static getInstance ( ) : LicenseManager {
1822 if ( ! LicenseManager . instance ) {
@@ -32,12 +36,22 @@ class LicenseManager {
3236 const cachedValid = localStorage . getItem ( this . STORAGE_KEY_CACHED_LICENSE ) ;
3337 const storedType = this . getStoredType ( ) ;
3438
39+ const oldStatus = this . activeLicense ;
40+ const oldType = this . type ;
41+
3542 if ( email && licenseKey && cachedValid === 'true' ) {
3643 this . activeLicense = true ;
3744 this . type = storedType || '' ;
45+ console . log ( '[LicenseManager] Restored from cache: active=true, type=' , this . type ) ;
3846 } else {
3947 this . activeLicense = false ;
4048 this . type = '' ;
49+ console . log ( '[LicenseManager] No cached license found (email:' , ! ! email , ', key:' , ! ! licenseKey , ', cached:' , cachedValid , ')' ) ;
50+ }
51+
52+ // Notify listeners if state changed (so already-mounted components update)
53+ if ( oldStatus !== this . activeLicense || oldType !== this . type ) {
54+ this . notifyListeners ( ) ;
4155 }
4256 }
4357
@@ -99,11 +113,14 @@ class LicenseManager {
99113 const email = this . getStoredEmail ( ) ;
100114 const licenseKey = this . getStoredLicenseKey ( ) ;
101115
116+ console . log ( '[LicenseManager] checkLicenseStatus: email=' , ! ! email , ', key=' , ! ! licenseKey ) ;
117+
102118 if ( ! email || ! licenseKey ) {
103119 const oldStatus = this . activeLicense ;
104120 this . activeLicense = false ;
105121 this . type = '' ;
106122 localStorage . removeItem ( this . STORAGE_KEY_CACHED_LICENSE ) ;
123+ console . log ( '[LicenseManager] No email/key stored, license cleared' ) ;
107124 if ( oldStatus !== this . activeLicense ) {
108125 this . notifyListeners ( ) ;
109126 }
@@ -135,6 +152,8 @@ class LicenseManager {
135152 const data = await response . json ( ) ;
136153 const isLicenseActive = data . hasActiveLicense === true || data . hasActiveLicense === 'True' ;
137154
155+ console . log ( '[LicenseManager] API response: active=' , isLicenseActive , ', email match=' , data . email === email , ', key match=' , data . linkedUserId === licenseKey ) ;
156+
138157 // Match user's email and licenseKey against the returned linkedUserId
139158 if ( isLicenseActive && data . email === email && data . linkedUserId === licenseKey ) {
140159 this . activeLicense = true ;
@@ -153,33 +172,39 @@ class LicenseManager {
153172 if ( data . purchaseDate ) {
154173 localStorage . setItem ( this . STORAGE_KEY_DATE , data . purchaseDate . toString ( ) ) ;
155174 }
175+ console . log ( '[LicenseManager] License validated: type=' , this . type ) ;
156176 } else {
157177 this . activeLicense = false ;
158178 this . type = '' ;
159179 localStorage . removeItem ( this . STORAGE_KEY_CACHED_LICENSE ) ;
180+ console . log ( '[LicenseManager] License validation failed' ) ;
160181 }
161182 } else {
162183 this . activeLicense = false ;
163184 this . type = '' ;
164185 localStorage . removeItem ( this . STORAGE_KEY_CACHED_LICENSE ) ;
186+ console . error ( '[LicenseManager] API returned non-ok status:' , response . status ) ;
165187 }
166188
167189 if ( oldStatus !== this . activeLicense || oldType !== this . type ) {
168190 this . notifyListeners ( ) ;
169191 }
170192 } catch ( error ) {
171- console . error ( 'Error checking license status:' , error ) ;
172- const oldStatus = this . activeLicense ;
173- this . activeLicense = false ;
174- localStorage . removeItem ( this . STORAGE_KEY_CACHED_LICENSE ) ;
175- // We keep the plan locally if we fail to check? Better to clear it to be safe or keep it cached?
176- // For safety/validity, if check fails, we assume no license.
177- // But maybe we should keep the cached values if network error?
178- // Current implementation clears it. I'll stick to that.
179- this . type = '' ;
180-
181- if ( oldStatus !== this . activeLicense ) {
182- this . notifyListeners ( ) ;
193+ console . error ( '[LicenseManager] Error checking license status:' , error ) ;
194+ // On network failure, preserve the cached license state rather than
195+ // invalidating a genuine license just because the user is offline.
196+ const cachedValid = localStorage . getItem ( this . STORAGE_KEY_CACHED_LICENSE ) ;
197+ if ( cachedValid === 'true' ) {
198+ console . log ( '[LicenseManager] Network error but cached license exists — keeping cached state' ) ;
199+ // Don't clear the license; keep the cached state so offline users
200+ // can still use premium features until the next successful check.
201+ } else {
202+ const oldStatus = this . activeLicense ;
203+ this . activeLicense = false ;
204+ this . type = '' ;
205+ if ( oldStatus !== this . activeLicense ) {
206+ this . notifyListeners ( ) ;
207+ }
183208 }
184209 }
185210 }
0 commit comments