Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if type(record)~="table" then record={} end
- local function prompt(q,y,n) --a function we will be using to get a yes or no answer for anything, very usefull
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print(q.." ["..y.." | "..n.."]")
- local prompt=read()
- if prompt==y then
- return y
- elseif prompt==n then
- return n
- end
- end
- end
- local function load(file) --a function to load the usernames from the file we saved last session
- if fs.exists(file) then
- record["users"]=textutils.unserialize(io.open(file):read) -- I will explain this later
- else
- print("user file not found")
- record["users"]={}
- end
- end
- local function createuser(user, file) --add users to the list and save them to the file
- if user==nil then
- print("Enter new username")
- local user=read()
- end
- term.clear()
- term.setCursorPos(1,1)
- print("enter password for "..user)
- local pass=read("*")
- record["users"][user]={}
- record["users"][user]["password"]=pass
- io.open(file,"w+"):write(textutils.serialize(record["users"]))
- print("Username created")
- end
- local function login() --the actual login function
- term.clear()
- term.setCursorPos(1,1)
- print("Enter your username")
- record["username"]=read()
- if record["users"][record["username"]]~=nil then
- term.clear()
- term.setCursorPos(1,1)
- print("Enter password for "..record["username"])
- local password=read("*")
- if record["users"][record["username"]]["password"]==password then
- print("LOGGED IN")
- return true
- else
- print("Incorrect password, restarting")
- sleep(2)
- reboot()
- end
- else --if record["users"][record["username"]] is nil (not already in the database)
- local answer=prompt("username: "..record["username"].." not found, would you like to add it?","y","n")
- if answer=="y" then
- createuser(record["username"], "userfile")
- sleep(2)
- reboot()
- elseif answer=="n" then
- reboot()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement