iteration
This commit is contained in:
72
lib/auth.lua
72
lib/auth.lua
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user