iteration

This commit is contained in:
2023-10-09 06:54:35 -04:00
parent e6ed55bcc7
commit bb0d27b755
9 changed files with 515 additions and 77 deletions

View File

@@ -1,3 +1,6 @@
-- Desc: Authentication library
-- Auth: Layla Manley
-- Link: https://gitea.layla.gg/layla/computercraft
settings.define("auth.token", {
description = "Authentication token",
type = "number",
@@ -11,6 +14,75 @@ settings.define("auth.server", {
})
function login(username, password)
local data = {
["action"] = "login",
["username"] = username,
["password"] = password
}
local auth_server_id = lns.lookup(settings.get("auth.server"))
rednet.send(auth_server_id, data, "auth")
local token = nil
while true do
id, msg = rednet.receive("auth")
if id == auth_server_id then
if msg == "invalid request" then
io.write("Invalid request\n")
return false
elseif msg == "user not found" then
io.write("User not found\n")
return false
elseif msg == "invalid password" then
io.write("Invalid password\n")
return false
else
token = msg
break
end
end
end
settings.set("auth.username", username)
settings.set("auth.token", token)
settings.save()
return true
end
function register(username, password)
local data = {
["action"] = "register",
["username"] = username,
["password"] = password
}
local auth_server_id = lns.lookup(settings.get("auth.server"))
rednet.send(auth_server_id, data, "auth")
while true do
id, msg = rednet.receive("auth")
if id == auth_server_id then
if msg == "invalid request" then
io.write("Invalid request\n")
return false
elseif msg == "user already exists" then
io.write("User already exists\n")
return false
else
io.write("Registered user " .. username .. "\n")
return true
end
end
end
end
function logout()
settings.set("auth.token", -1)
settings.save()
io.write("Logged out\n")
end
function get_token()
return settings.get("auth.token")
end