@@ -25,6 +25,7 @@ type Payload = {
2525 addDevice : { licenseId : string }
2626 fetchDevices : { licenseId : string }
2727 upgradeLicense : { oldLicense : string , locale : string }
28+ checkDevice : { device : string }
2829 genDeviceString : { } ,
2930}
3031
@@ -142,7 +143,7 @@ export function showPremium (tab?: PremiumTab) {
142143 ga . logEvent ( 'yn_premium_show' , { purchased : getPurchased ( ) } )
143144}
144145
145- export function getLicenseToken ( throwError = false ) {
146+ export function getLicenseToken ( ) {
146147 logger . debug ( 'getLicenseToken' )
147148 try {
148149 const tokenStr = getSetting ( 'license' )
@@ -167,10 +168,6 @@ export function getLicenseToken (throwError = false) {
167168
168169 return syncCacheLicenseToken ( token )
169170 } catch ( error ) {
170- if ( throwError ) {
171- throw error
172- }
173-
174171 logger . error ( 'getLicenseToken' , error )
175172 }
176173
@@ -217,11 +214,21 @@ async function setLicense (licenseId: string) {
217214 }
218215}
219216
217+ async function checkDevice ( device : string ) {
218+ try {
219+ await requestApi ( 'checkDevice' , { device } )
220+ } catch ( error ) {
221+ cleanLicense ( )
222+ throw error
223+ }
224+ }
225+
220226export async function refreshLicense ( opts ?: { throwError ?: boolean } ) {
221227 logger . debug ( 'refreshLicense' , opts )
222228 try {
223229 const token = getLicenseToken ( )
224230 if ( token ) {
231+ await checkDevice ( token . device )
225232 await setLicense ( token . licenseId )
226233 }
227234 } catch ( error ) {
@@ -242,7 +249,12 @@ export async function activateLicense (licenseId: string) {
242249export async function activateByTokenString ( tokenString : string ) {
243250 logger . debug ( 'activateByToken' , tokenString )
244251 await setSetting ( 'license' , tokenPrefix + tokenString )
245- getLicenseToken ( true )
252+ const token = getLicenseToken ( )
253+ if ( token ) {
254+ await checkDevice ( token . device )
255+ } else {
256+ throw new Error ( 'INVALID_LICENSE' )
257+ }
246258}
247259
248260function checkLicenseStatus ( ) {
0 commit comments