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

Login system

Started by lieudusty, 03 August 2012 - 04:19 AM
lieudusty #1
Posted 03 August 2012 - 06:19 AM
Hi everyone! :ph34r:/>/>

I'm making a program that requires a register and login system. I have no idea how I would make this. Is there a way to write a string to a file and check that string later on? Can someone please help? Thanks! :)/>/>
tfoote #2
Posted 03 August 2012 - 06:23 AM
First off… The WIKI is your FRIEND!!! I love the Wiki.
Also Get familiar with the APIs
If it were me I would go all our on these APIs: fs, rednet, table.
I Would also end up making a server for these. But first you will mostlikely want to use the fs api. It is a file editor. Also in the table api you can store stuff in tables. (du). and the rednet api is mainly for the server.
lieudusty #3
Posted 03 August 2012 - 06:32 AM
Wow quick reply! The program I was making was a file server with login and registering capabilities.
Rsstn #4
Posted 03 August 2012 - 09:01 AM
The best way to store the usernames and passwords is using the FS API (more specifically the 'fs.open()' command). This means that you don't have to hard-code the passwords, so users can change their passwords using another program (called something like 'changepassword'). You should give those FS API pages a good look over, and see what you can use from it.
KaoS #5
Posted 03 August 2012 - 10:45 AM
what I would suggest is creating a table for all users, all of my computers have a main table in which EVERYTHING is stored (rather than using variables I assign a table value) so that I can export everything to one string. so:


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

then you call the functions:


load("userfile")
if login()~=true then
print("login process failed, restarting")
sleep(2)
reboot()
else
print("Welcome "..record["username"])
end

THIS IS NOT TESTED

with such a long program I have probably made a mistake somewhere but you get the general idea, what I am trying to say is that using tables to store data and then serializing them and storing them is the best way to store data

lol, thinking back I am not entirely sure why I just wrote an entire program when you were just asking for help… :ph34r:/>/>
KaoS #6
Posted 03 August 2012 - 10:56 AM
oh yes, I didn't make it create the file if it does not exist, add that or make the file 'userfile' on the computer, you can make a host pc store the file and send it to all of the computers on the server if you like… you can really do whatever you want with it