Login with Roaming Profiles

From ComputerCraft Wiki
Revision as of 14:46, 9 August 2012 by (Talk) (Client Code)

Jump to: navigation, search

This tutorial covers how to make a password server and a password client. The server will host a Lua table of usernames and passwords and the client will remotely connect each start up, ask the user for their username and password and compare it to that on the password server.

Setting Up

As I'm sure you're aware, writing code directly into the computer terminal can be difficult, slow, and therefore annoying. I highly recommend writing/copying the following code into a Lua editor and then using FTP to transfer the documents across to the computer or disk. Please ensure you do not add the .lua extension.

First craft two or more computers. One will act as your password server, any others will be connecting to that computer. Find the computer ID of your password server. This is important as we don't want to broadcast password requests and replies. In the following code I have also locked the requests to certain computers to ensure security but it is not a needed component of the code. If you decide to take the security as well, make a note of all of the ID's of the client computers. I found it useful to give each computer a label and then search for labels.txt on the server to map it out easily.

Each computer you craft needs a rednet modem.

The Code

Server Code

os.pullEvent = os.pullEventRaw
print("This is a password server. There is no user interaction here.")
print("Please find a computer and login there.")   
local firstCycle = true
local validSender = false
local modemSide = "left" -- change to the side of the computer your modem is on
local valid = false
users = {}
users = { username1="password", username2="password2" }
senders = {}
senders = { 13, 14, 15, 17 } -- computer ID's of the computers you want to accept requests from
function bootUp()
while true do 
 validSender = false
 if firstCycle then
 senderId, message, distance = rednet.receive()
 for i,v in ipairs(senders) do
  if v == senderId then
   validSender = true
 if validSender then
  for k,v in pairs(users) do
   if message == k then
    valid = true
	password = v
    valid = false
  if valid then
   rednet.send(senderId, password)
   rednet.send(senderId, "Not Valid")

Client Code

os.pullEvent = os.pullEventRaw
local locker = true
local failed = true
local attempted_login = true
local password_server = 17 -- change to the ID of your password server computer
rednet.open("left") -- change to the side your rednet modem is on
while locker do
 attempted_login = false
 print("Welcome to a USERS PC : Roaming Profile Enabled")
 print("What would you like to do?")
 print("[1] Login (*)")
 print("[2] Shutdown")
 write("> ")
 local input = read()
 if input == "2" then
 elseif input == "1" then
  attempted_login = true
  print("Please login...")
  write("Username: ")
  local username = read()
  write("Password: ")
  local password = read("*")
  rednet.send(password_server, username)
  senderId, message, distance = rednet.receive(5)
  if password == message then
   failed = false
   locker = false
   print("Welcome ", username)
   print("Not authorised.")
  print("Command not recognised...")

Thoughts and Notes

If you change the client code, to "startup" and put it in the root directory, it works very well as a login system. You may also want to not put in the line

os.pullEvent = os.pullEventRaw -- disables Ctrl+T

as if it doesn't work first time it can be difficult to get out of the program.

If you run Tekkit, find the 'World Anchor' block. If your server can afford the resources, place the block within a 3x3 grid of your password server. It will ensure that even if you leave the chunk, the password server still is operational. Otherwise you may find that you cannot login because you've left the chunk and the password server is no longer dealing with requests.