←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

How would I set up this

Sluethen's Photo Sluethen 21 Jul 2017

So How would I make a login system that can read user's from a file? I'm new to this. I know how to make a login system but how would I Make it read and write to a file? I want it to read username's and password's from a file, but I'm not sure how.
Here is my current lock system. https://pastebin.com/sXnCsYyn
Quote

Dave-ee Jones's Photo Dave-ee Jones 21 Jul 2017

Well, one way of doing it is saving a table full of usernames/passwords to a file, then importing the table into the program.
Look at textutils.seralize() and textutils.unserialize().

Another way is having a Users directory which has user files in it. So the name of the file is the username and the password would be saved in that file, raw or as a hash.
E.g.
- Users (directory)
  + admin (content: password)
  + tommy (content: theawesomeguy123)
  + jack (content: jh1240d845)

Then you can make a for loop that looks for a file in that folder with the name entered by the user logging in on the computer. Fairly easy if you know how to read/write to a file and use a for loop. :)
Quote

Sluethen's Photo Sluethen 22 Jul 2017

I don't know how to read and write to a file(well) I know how to sorta write to a file but reading is nothing I've learned.... I should look into reading and writing files

I'm also having a bit of trouble understanding how I would make it read each user
Quote

KingofGamesYami's Photo KingofGamesYami 22 Jul 2017

For anything file-related, check out the FS API.

As for how to differentiate between users, you have a few options:

Option 1: Serialized Table - A table of users in the format tbl[ username ] = password. The table would be serialized and unserialized as necessary to write or read it from the file.

Option 2: Specialized file format - you could format your file in a particular way, eg. "username:password;username:password", then use string manipulation to extract the desired pairings. This would reduce file size compared to option 1, but is more difficult to implement.

Option 3: Multiple files - you could create a new file for each user (as suggested by Dave-ee Jones), where the file's name is the username and the contents is the password for that user.

Special Considerations: The majority of programs which store passwords in files make use of sha256, a hashing algorithm. This prevents someone from gaining access to the files on the computer and reading your password.
Quote

Krul__Tepes's Photo Krul__Tepes 22 Jul 2017

this is what i need to figure out to make my phone program more advance
Quote

Sluethen's Photo Sluethen 22 Jul 2017

I've went and gone with Dave-ee's way, but I'm having trouble with making it write on other lines. for a Admin status. I've done it before but I'm either missing something or I'm failing.
Quote

Sluethen's Photo Sluethen 22 Jul 2017

It might help XD
Quote

KingofGamesYami's Photo KingofGamesYami 23 Jul 2017

To write on a new line you just need to add a "\n" character to the end of the previous line.

For example,
"Hello\nWorld"
Would be written as:
Hello
World
Quote

Sluethen's Photo Sluethen 23 Jul 2017

Edit:Post has been eaten
Edited by Sluethen, 23 July 2017 - 03:35 AM.
Quote

KingofGamesYami's Photo KingofGamesYami 23 Jul 2017

Sluethen, I would highly advise you to stop giving false advice. file.writeLine does not accept multiple parameters. Your example will write on multiple lines, but not necessarily the ones specified.

In addition, your comment on using "a" is extremely misleading. Append mode is very different from normal write mode!
Edited by KingofGamesYami, 23 July 2017 - 02:48 AM.
Quote

Sluethen's Photo Sluethen 23 Jul 2017

I'm not quite sure why I posted that. .-. I ate it.
Edited by Sluethen, 23 July 2017 - 04:12 AM.
Quote