This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
xtbs's profile picture

Storing Multiple User Accounts

Started by xtbs, 22 April 2016 - 02:23 PM
xtbs #1
Posted 22 April 2016 - 04:23 PM
I have a bit of a complicated question, I think.

On my friends server I am working on programming a user account system that relies off of a server so that computers that are installed with the software I am writing are able to give certain users permissions, log event data, etc.

Anyways the biggest problem I am coming across is trying to set up these accounts live. So I have a prgram on the server that will store usernames, passwords, etc. I sort of know how to store data to a file and reference it in a different program but the problem I am coming across is the part that includes multiple users. Right now because of the way the variables are, the program will only store one user at a time and then overwrite that data if a new user/ pass is put in. IRL I would use a something like mysql and since cc doesnt exactly use that, How could I implement a system that stores new users without overwriting existing users.

sort of what I have for code right now is.


local name
local pass
textutils.slowPrint("Enter Username")
name = read()
textutils.slowPrint("Enter Password")
pass = read("*")
local account = {}
account.name = name
account.pass = pass
local output = textutils.serialise(account)
local handle = assert(fs.open("account", "w"), "Could Not Store Account")
handle.write(output)
handle.close()


and the program that I use to reference the data is….


local handle = assert(fs.open("account", "r"), "Could Not Load Account")
local input = handle.readAll()
handle.close
local account = textutils.unserialise(input)
print(account.name)
print(account.pass)

I did use code from another post, so when i reference assert(fs.open("account", "r"), i dont know what the "r" is used for nor what assert is referencing. so if someone could enlighten me on that I would appreciate it, (I do understand fs.open is referencing the file and that the "account" is the file name.)
I know the code for the multi accounts will be considerably more complex, but I do want to give it a try.


Thank you in advance for the help :)/>
TYKUHN2 #2
Posted 22 April 2016 - 08:09 PM
Assert basically is "If this doesn't work use the second parameter." I'd look at the Lua wiki for that (google "Lua Assert Function")

You really should look at the FS API especially the fs.open() function.

Also since I am incredibly lazy I did one file per account on one of my websites so I don't have to iterate; though it is generally not recommended.
Morganamilo #3
Posted 22 April 2016 - 09:14 PM
try

local name
local pass
local handle = assert(fs.open("account", "r")
local accounts = textutils.unserialise(handle.readAll())
handle.close()
textutils.slowPrint("Enter Username")
name = read()
textutils.slowPrint("Enter Password")
pass = read("*")
accounts[name] = pass
local handle = assert(fs.open("account", "w"), "Could Not Store Account")
handle.write(textutils.serialise(accounts))
handle.close()

and


local handle = assert(fs.open("account", "r"), "Could Not Load Account")
local accounts = textutils.unserialise((handle.readAll())
handle.close()

name =read()
password = read()
if accounts[name] == password then
	 print("match")
else
	print("noMatch")
end

I have not actually ran this but I trust you to fix the silly mistakes if there are any.
Edited on 22 April 2016 - 07:15 PM
xtbs #4
Posted 22 April 2016 - 10:12 PM
It worked for the most part, but I had to change the "w" to "a" when it was writing to file because it would overwrite my previous input. and had to initialize accounts as
local accounts = {} and get rid of the handle event at the beginning because it was throwing a could not load accounts.

one more question, if I wanted to store more than a password, like have a username, password, and isAdmin how could i do that?