Skip to content

User Login System

Alex May edited this page Mar 26, 2020 · 1 revision

The following script will implement a basic user registration and login system

-- ArtMOO Script --

;s='';for i=1,64 do s = s .. string.char( 65 + ( math.random( 0, 1 ) * 32 ) + math.random( 0, 25 ) ) end; moo.system:propadd( 'salt', s )
;moo.system:prop( 'salt' ).r = false
;moo.system:prop( 'salt' ).w = false

;moo.create( moo.root )
;moo.last.name = 'Generic Player'
;moo.last:propadd( 'password', '' )

;moo.root:propadd( 'generic_player', moo.last )

;moo.me.parent = moo.root.generic_player
;moo.me.password = moo.hash( moo.system.salt .. 'password' )

;----------------------------------------------------------------------

;moo.system:verb( 'do_login_command' ):program()
if #moo.args == 0 then
   moo.notify( "Welcome to the MOO" )
   return
end

local command = moo.args[ 1 ]

moo.notify( "command: " .. command )

if command == 'connect' then
   if #moo.args ~= 2 then
      moo.notify( "Usage: connect <username> <password>" )
      return
   end

   local user = moo.find_player( moo.args[ 1 ] )
   
   if not user then
      moo.notify( "Unknown user account" )
	  return
   end
   
   local pass = user:prop( 'password' )
   
   if not pass then
      moo.notify( "That isn't a valid username" )
      return
   end
   
   if moo.hash( moo.system.salt .. moo.args[ 2 ] ) ~= pass:value() then
      moo.notify( "Password isn't correct" )
	  return
   end
   
   moo.notify( "Good to see you again " .. user.name )
   
   return( user )
end

if command == 'new' then
   moo.notify( "Enter your username:" )
   moo.read( moo.object, 'login_new' )
   return
end

moo.notify( "Unknown command " .. command )

.

;----------------------------------------------------------------------

;moo.system:verbadd( 'login_new' ):program()
local args = { ... }

if #args == 0 then
	moo.notify( "no username" )
	return
end

if #args > 1 then
	moo.notify( "<red>usernames need to be one word</red>" )
	return
end

if moo.find_player( args[ 1 ] ) ~= nil then
	moo.notify( "<red>that username is already taken</red>" )
	return
end


moo.notify( "Enter your password:" )

moo.read( moo.object, "login_new_password", { password = true }, ... )
.

;----------------------------------------------------------------------

;moo.system:verbadd( 'login_new_password' ):program()
local args = { ... }

if #args < 2 then return end

if #args == 2 then
	moo.notify( "Enter your password again:" )

	moo.read( moo.object, moo.verb, { password = true }, ... )
	
	return
end

if #args == 3 then
	local p2, p1, un = ...

	if p1 ~= p2 then
		moo.notify( "Sorry, the passwords do not match" )
		return
	end

	local p = moo.create( moo.root.generic_player )

	p.name     = un
	p.password = moo.hash( moo.system.salt .. p1 )
	p.player   = true
	
	p.location = o( 2 )
	
	return p
end
.
Clone this wiki locally