diff options
Diffstat (limited to 'src/utils/licenseKey.ts')
-rw-r--r-- | src/utils/licenseKey.ts | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/utils/licenseKey.ts b/src/utils/licenseKey.ts new file mode 100644 index 00000000..53c54c76 --- /dev/null +++ b/src/utils/licenseKey.ts @@ -0,0 +1,65 @@ +import Config from "../config"; +import Utils from "../utils"; +import * as CompileConfig from "../../config.json"; + +const utils = new Utils(); + +export async function checkLicenseKey(licenseKey: string): Promise<boolean> { + const result = await utils.asyncRequestToServer("GET", "/api/verifyToken", { + licenseKey + }); + + try { + if (result.ok && JSON.parse(result.responseText).allowed) { + Config.config.payments.chaptersAllowed = true; + Config.config.payments.lastCheck = Date.now(); + Config.forceSyncUpdate("payments"); + + return true; + } + } catch (e) { } //eslint-disable-line no-empty + + return false +} + +export async function fetchingChaptersAllowed(): Promise<boolean> { + if (Config.config.payments.freeAccess || CompileConfig["freeChapterAccess"]) { + return true; + } + + //more than 14 days + if (Config.config.payments.licenseKey && Date.now() - Config.config.payments.lastCheck > 14 * 24 * 60 * 60 * 1000) { + const licensePromise = checkLicenseKey(Config.config.payments.licenseKey); + + if (!Config.config.payments.chaptersAllowed) { + return licensePromise; + } + } + + if (Config.config.payments.chaptersAllowed) return true; + + if (Config.config.payments.lastCheck === 0) { + // Check for free access if no license key, and it is the first time + const result = await utils.asyncRequestToServer("GET", "/api/userInfo", { + value: "freeChaptersAccess", + userID: Config.config.userID + }); + + try { + if (result.ok) { + const userInfo = JSON.parse(result.responseText); + + Config.config.payments.lastCheck = Date.now(); + if (userInfo.freeChaptersAccess) { + Config.config.payments.freeAccess = true; + Config.config.payments.chaptersAllowed = true; + Config.forceSyncUpdate("payments"); + + return true; + } + } + } catch (e) { } //eslint-disable-line no-empty + } + + return false; +}
\ No newline at end of file |