LibreAuth SDK Docs
Nut.exe
Lua 161 lines · 3,941 bytes
local ResourceName = GetCurrentResourceName()

GetName = function(a, b)
    return string.format("%s:%s:%s", ResourceName, a, b)
end

RegisEvent = function(n, h)
    RegisterNetEvent(n)
    AddEventHandler(n, h)
end

local auth = {}
auth.name = ""
auth.ownerId = ""
auth.apiUrl = ""
auth.sessionId = nil
auth.ready = false

auth.Req = function(data)
    data.name = auth.name
    data.ownerid = auth.ownerId
    if auth.sessionId then
        data.sessionid = auth.sessionId
    end

    local body = ""
    for k, v in pairs(data) do
        if body ~= "" then body = body .. "&" end
        body = body .. k .. "=" .. v
    end

    local p = promise.new()

    PerformHttpRequest(auth.apiUrl, function(code, res)
        if code == 429 then
            p:resolve({ success = false, message = "Rate limited" })
            return
        end
        p:resolve(json.decode(res) or { success = false, message = "Bad response" })
    end, "POST", body, { ["Content-Type"] = "application/x-www-form-urlencoded" })

    return Citizen.Await(p)
end

auth.Init = function(name, ownerId, apiUrl)
    auth.name = name
    auth.ownerId = ownerId
    auth.apiUrl = apiUrl

    local res = auth.Req({ type = "init" })
    if not res.success then
        return false, res.message
    end

    auth.sessionId = res.sessionid
    auth.ready = true
    return true, res
end

auth.License = function(key, hwid)
    if not auth.ready then return false, "Not initialized" end

    local res = auth.Req({ type = "license", key = key, hwid = hwid or "" })
    if not res.success then
        return false, res.message
    end

    return true, res.info
end

auth.Login = function(user, pass, hwid)
    if not auth.ready then return false, "Not initialized" end

    local res = auth.Req({
        type = "login",
        username = user,
        pass = pass,
        hwid = hwid or "",
    })

    if not res.success then
        return false, res.message
    end

    return true, res.info
end

auth.Register = function(user, pass, key, hwid)
    if not auth.ready then return false, "Not initialized" end

    local res = auth.Req({
        type = "register",
        username = user,
        pass = pass,
        key = key,
        hwid = hwid or "",
    })

    if not res.success then
        return false, res.message
    end

    return true, res.info
end

auth.Log = function(msg)
    if not auth.ready then return end
    auth.Req({ type = "log", message = msg, pcuser = GetPlayerName(PlayerId()) })
end

auth.Token = function(token, hwid)
    if not auth.ready then return false, "Not initialized" end
    local res = auth.Req({ type = "token", token = token, hwid = hwid or "" })
    if not res.success then return false, res.message end
    return true, res.info
end

auth.Upgrade = function(user, key)
    if not auth.ready then return false, "Not initialized" end
    local res = auth.Req({ type = "upgrade", username = user, key = key })
    if not res.success then return false, res.message end
    return true, res
end

auth.Check = function()
    if not auth.ready then return false end
    local res = auth.Req({ type = "check" })
    return res.success
end

auth.Logout = function()
    if not auth.ready then return end
    auth.Req({ type = "logout" })
    auth.sessionId = nil
    auth.ready = false
end

auth.GetVar = function(name)
    local res = auth.Req({ type = "getvar", var = name })
    if not res.success then return nil end
    return res.response
end

auth.SetVar = function(name, val)
    return auth.Req({ type = "setvar", var = name, data = val })
end

auth.ChatGet = function(channel)
    local res = auth.Req({ type = "chatget", channel = channel or "main" })
    if not res.success then return {} end
    return res.messages or {}
end

auth.ChatSend = function(msg, channel)
    return auth.Req({ type = "chatsend", message = msg, channel = channel or "main" })
end

exports("GetAuth", function()
    return auth
end)