bagindo / tauri-plugin-keygen

Tauri plugin for Keygen.sh licensing
MIT License
8 stars 3 forks source link

Sometimes getLicense returns null after validateCheckoutKey #4

Open lucabrini opened 1 week ago

lucabrini commented 1 week ago

Hello, I'm trying to use offline licensing. I'm currently validating the key using validateCheckoutKey() but sometimes it's seems that the machine lic file is not being saved on the device because the next time the app is opened getLicense() returns null.

The problem shows up when app is fully reopen. If I just reload the app using right click -> reload of tauri, the license is kept in memory.

https://github.com/user-attachments/assets/02b262ad-1551-413c-b04b-fc162e9acca5

Here's the LicenseManager class that I've written.

import {
    getLicense,
    resetLicense,
    validateCheckoutKey,
    type KeygenError,
    type KeygenLicense
} from 'tauri-plugin-keygen-api';

class LicenseManager {
    private static _instance: LicenseManager | null = null;

    private _license : KeygenLicense | null = null;
    private _ready: boolean = false;

    private constructor() {}

    public async init() {
        console.log('LicenseManager init');

        if (this._ready) return;

        const license = await getLicense(); // sometimes this is null
                console.log('License:', license);
        if (license) {
            this._license = license;
        }
        this._ready = true;
    }

    public async validate(key: string) {
        try {
            let validationResults = await validateCheckoutKey({
                key,
                entitlements: ['ADMIN'],
                ttlForever: true
            });
            this._license = validationResults;
            console.log('Validation:', validationResults);
            return true;
        } catch (e) {
            alert((e as KeygenError).detail);
            return false;
        }
    }

    public async logout() {
        await resetLicense();
    }

    public static get instance(): LicenseManager {
        if (!LicenseManager._instance) {
            LicenseManager._instance = new LicenseManager();
        }
        return LicenseManager._instance;
    }

    public get license(): KeygenLicense | null {
        return this._license;
    }

    public get isLicensed(): boolean {
        const valid = this._license?.valid ?? false;
        return valid;
    }
}

export default LicenseManager;
lucabrini commented 1 week ago

It seems like that waiting around one minute it starts working

lucabrini commented 1 week ago

After an entire day of debugging I've found the issue. I'm gonna open a PR right now

bagindo commented 1 week ago

Hi @lucabrini good catch! I'm merging your PR. Thanks!

lucabrini commented 1 week ago

Heiii! Niceee! You're welcome sir!

I saw that the build has failed for macos due to the time library. I've also met this issue in my project. Time cargo package needs to be updated to 0.3.36

bagindo commented 1 week ago

@lucabrini got it. I'll update it along with v2 update.