Skip to content

Commit

Permalink
add api to validate license (#158)
Browse files Browse the repository at this point in the history
* add api to validate license

* add api to validate license
  • Loading branch information
domechn authored Nov 11, 2023
1 parent 71463a1 commit 8341cd9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/components/system-info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import HideIcon from "@/assets/icons/hide-icon.png";
import { Input } from "./ui/input";
import { Button } from "./ui/button";
import { ReloadIcon } from "@radix-ui/react-icons";
import { LicenseCenter } from "@/middlelayers/license";
import toast from "react-hot-toast";

const App = () => {
const [version, setVersion] = useState<string>("0.1.0");
Expand Down Expand Up @@ -43,7 +45,19 @@ const App = () => {
}
setSaveLicenseLoading(true);

saveLicense(license).then(() => {});
LicenseCenter.getInstance()
.validateLicense(license)
.then((result) => {
if (result.isPro) {
saveLicense(license);
toast.success("License Key Saved");
} else {
toast.error("Invalid License Key");
}
})
.finally(() => {
setSaveLicenseLoading(false);
});
}

function onLicenseInputChange(val: string) {
Expand Down Expand Up @@ -102,7 +116,7 @@ const App = () => {
onClick={onSaveLicenseClick}
disabled={saveLicenseLoading || !licenseChanged}
>
{saveLicenseLoading ?? (
{saveLicenseLoading && (
<ReloadIcon className="mr-2 h-4 w-4 animate-spin" />
)}
Active
Expand Down
39 changes: 39 additions & 0 deletions src/middlelayers/license.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { getClientID } from '@/utils/app'
import { sendHttpRequest } from './datafetch/utils/http'

export class LicenseCenter {
private static instance: LicenseCenter

private readonly validateEndpoint = "https://track3-pro-api.domc.me/api/license/validate"

private constructor() { }

public static getInstance(): LicenseCenter {
if (!LicenseCenter.instance) {
LicenseCenter.instance = new LicenseCenter()
}

return LicenseCenter.instance
}

public async validateLicense(license: string): Promise<{
isPro: boolean,
}> {
try {
const resp = await sendHttpRequest<{
isPro: boolean
}>("POST", this.validateEndpoint, 10000, {
"x-track3-client-id": await getClientID(),
'x-track3-api-key': license
})

return {
isPro: resp.isPro
}
} catch (e) {
return {
isPro: false
}
}
}
}

0 comments on commit 8341cd9

Please sign in to comment.