LibreAuth SDK Docs
Nut.exe
JavaScript 60 lines · 1,684 bytes
class LibreAuth {
    constructor(name, ownerId, apiUrl) {
        this.name = name;
        this.ownerId = ownerId;
        this.apiUrl = apiUrl.replace(/\/$/, '') + '/';
        this.sessionId = null;
    }

    async Init() {
        const res = await this.Req({ type: 'init' });
        if (!res.success) throw new Error(res.message);
        this.sessionId = res.sessionid;
        return res;
    }

    async Login(user, pass, hwid = '') {
        return this.Auth({ type: 'login', username: user, pass, hwid });
    }

    async Register(user, pass, key, hwid = '') {
        return this.Auth({ type: 'register', username: user, pass, key, hwid });
    }

    async License(key, hwid = '') {
        return this.Auth({ type: 'license', key, hwid });
    }

    async Var(name) {
        const res = await this.Req({ type: 'var', varid: name });
        return res.success ? res.message : null;
    }

    async Log(msg) {
        await this.Req({ type: 'log', message: msg, pcuser: '' });
    }

    async Auth(data) {
        const res = await this.Req(data);
        if (!res.success) throw new Error(res.message);
        return res.info;
    }

    async Req(data) {
        data.name = this.name;
        data.ownerid = this.ownerId;
        if (this.sessionId) data.sessionid = this.sessionId;

        const res = await fetch(this.apiUrl, {
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: new URLSearchParams(data),
        });

        if (res.status === 429) throw new Error('Rate limited');
        return res.json();
    }
}

if (typeof module !== 'undefined') module.exports = LibreAuth;