Advertisement
fatboychummy

Example_Reddit_1

Jul 9th, 2021
1,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1.     -- client
  2.     local validKeys = {}
  3.     local users = {
  4.       ["fatboychummy"] = {
  5.         password = "SomeHashedPassword",
  6.         balance = 64
  7.       }
  8.     }
  9.     while true do
  10.       local senderID, message = rednet.receive("Banker") -- listens for rednet message with protocol "Banker"
  11.       if type(message) == "table" then
  12.         if message.action == "login" then
  13.           if users[message.user] and users[message.user].password == message.password then
  14.             -- login ok, return new key to user and save the login key
  15.             local newKey = "blablabla"
  16.             validKeys[newKey] = {
  17.               expiry = os.clock() + 30 -- key valid for 30 seconds.
  18.               user = users[message.user]
  19.             }
  20.             rednet.send(senderID, {
  21.               ok = true,
  22.               key = newKey
  23.             }, "Banker")
  24.           else
  25.             -- login NOT okay, return failure
  26.             rednet.send(senderID, {
  27.               ok = false,
  28.               error = "Login failure."
  29.             }, "Banker")
  30.           end
  31.         else
  32.           -- check valid key
  33.           if validKeys[message.key] and validKeys[message.key].expiry > os.clock() then
  34.             -- key valid
  35.             if message.action == "getBalance" then
  36.               rednet.send(senderID, {
  37.                 ok = true,
  38.                 balance = validKeys[message.key].user.balance
  39.               }, "Banker")
  40.             else -- invalid action
  41.               rednet.send(senderID, {
  42.                 ok = false,
  43.                 error = "Invalid action: " .. message.action
  44.               }, "Banker")
  45.             end
  46.           else
  47.             -- key not valid
  48.             rednet.send(senderID, {
  49.               ok = false,
  50.               error = "You are not logged in!"
  51.             }, "Banker")
  52.           end
  53.         end
  54.       end
  55.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement