Jump to content

[Help]Account-system


Dimos7

Recommended Posts

local connection = exports["mysql"]:getConnection()

function loginHandler(thePlayer, username, password)
   local account = getAccount(username)
   local hashedPassword = getAccountData(account, "hashedPassword")
   local query = dbQuery(connection, "SELECT * FROM accounts WHERE username and password =?", tostring(username), tostring(hashedPassword))
   local result, num_affected_row = dbPoll(query, -1)
   if account ~= nil and passwordVerify(password, hashedPassword, function(isVaild) if not isVaild then exports["notification-system"]:sendClientMessage("ERROR :Password is invaild!", thePlayer, 255, 0, 0) end end) and num_affected_row > 1 then
      if (logIn(thePlayer, account, hashedPassword) == true ) then
	       triggerClientEvent(thePlayer, "hideLoginWindow", root)
		   setElementData(thePlayer, "account:id", result.id)
		   dbExec(connection, "UPDATE accounts SET lastlogin=? WHERE username=?", "NOW()", tostring(username))
		   exports["notification-system"]:sendClientMessage("INFO: You successfuly logged in!", thePlayer ,0, 255, 0)
	  else
	     exports["notification-system"]:sendClientMessage("ERROR: That account is already logged in!", thePlayer, 255, 0, 0) 
	  end
	else
	  exports["notification-system"]:sendClientMessage("ERROR: Username or passwrod is invailed!", thePlayer, 255, 0, 0)
   end
end

function reigsterHandler(thePlayer, username, email, password)
    local account = getAccount(username)
	local query = dbQuery(connection, "SELECT *FROM accounts WHERE username =?",tostring(username))
	local result, num_affected_row = dbPoll(query, -1)
	if account ~= nil and num_affected_row > 1 then
	   exports["notification-system"]:sendClientMessage("ERROR: That username is already exist!", thePlayer, 255, 0, 0)
	else
	    passwordHash(password, "bcrypt", {}, function(hashedPassword)
		    local account = addAccount(username,hashedPassword)
		    setAccountData(account, "hashedPassword", hashedPassword)
		    local ip = getPlayerIP(thePlayer)
		    local serial = getPlayerSerial(thePlayer)
		    local r = getRealTime()
		    local regDate = ("%04d-%02d-%02d %02d:%02d:%02d"):format(r.year+1900, r.month+1, r.monthday, r.hour, r.minute, r.second)
		    dbExec(connection, "INSERT INTO accounts (username, password ,email, ip, serial, registerdate) VALUES(?, ?, ?, ?, ?, ?)", tostring(username), tostring(hashedPassword), tostring(email), tostring(ip), tostring(serial), tostring(regDate)) 
		    triggerClientEvent(thePlayer, "hideRegisterWindow", root)
		    exports["notification-system"]:sendClientMessage("INFO: You successfuly register!", thePlayer, 0, 255, 0)
		end)
	end
end
addEvent("submitLogin", true)
addEvent("submitRegister", true)
addEventHandler("submitLogin", root, loginHandler)
addEventHandler("submitRegister", root, reigsterHandler)
local sw, sh = guiGetScreenSize()
showChat(false)
setPlayerHudComponentVisible("all", false)

local GUIEditor = {
    window = {},
	label = {},
	edit = {},
	memo = {},
	checkbox = {},
	button = {},
}


function createLoginWindow()
    GUIEditor.window[1] = guiCreateWindow((sw-662)/2, (sh-285)/2, 662, 285, "Login", false)
	guiWindowSetSizable(GUIEditor.window[1], false)
	guiWindowSetMovable(GUIEditor.window[1], false)
	GUIEditor.label[1] = guiCreateLabel(20, 49, 84, 19, "Username:", false, GUIEditor.window[1])
	guiSetFont(GUIEditor.label[1], "default-blod-small")
	GUIEditor.edit[1] = guiCreateEdit(94, 39, 156, 36, "", false, GUIEditor.window[1])
	guiEditSetMaxLength(GUIEditor.edit[1], 20)
	GUIEditor.label[2] = guiCreateLabel(20, 95, 84, 19, "Password:", false, GUIEditor.window[1])
	guiSetFont(GUIEditor.label[2], "default-blod-small")
	GUIEditor.edit[2] = guiCreateEdit(94, 85, 156, 36, "", false, GUIEditor.window[1])
	guiEditSetMaxLength(GUIEditor.edit[2], 50)
	guiEditSetMasked(GUIEditor.edit[2], true)
	GUIEditor.button[1] = guiCreateButton(94, 185, 156, 36, "Login", false, GUIEditor.window[1])
	GUIEditor.button[2] = guiCreateButton(94, 231, 156, 36, "Register", false, GUIEditor.window[1])
	GUIEditor.checkbox[1] = guiCreateCheckBox(94, 136, 149, 15, "Rememeber information", false, false, GUIEditor.window[1])
	GUIEditor.memo[1] = guiCreateMemo(296, 39, 351, 231, "Hey, you there!\n\nWelcome to the great experience of Countyside Roleplay.\n\nWe hope to enjoy your time here", false, GUIEditor.window[1])
	guiMemoSetReadOnly(GUIEditor.memo[1], true)
	addEventHandler("onClientGUIClick", GUIEditor.button[1], onClientSubmitLogin, false)
	addEventHandler("onClientGUIClick", GUIEditor.button[2], createRegisterWindow, false)
	addEventHandler("onClientGUIClick", GUIEditor.checkbox[1], onClientSumbitSaveInformaiton, false)
end

function createRegisterWindow()
    guiSetVisible(GUIEditor.window[1], false)
    GUIEditor.window[2] = guiCreateWindow((sw-311)/2, (sh-285)/2, 311, 285, "Registration", false)
	guiWindowSetMovable(GUIEditor.window[2], false)
	guiWindowSetSizable(GUIEditor.window[2], false)
	GUIEditor.label[3] = guiCreateLabel(20, 49, 84, 19, "Username:", false, GUIEditor.window[2])
	guiSetFont(GUIEditor.label[3], "default-blod-small")
	GUIEditor.edit[3] = guiCreateEdit(94, 39, 156, 36, "", false, GUIEditor.window[2])
	guiEditSetMaxLength(GUIEditor.edit[3], 20)
	GUIEditor.label[4] = guiCreateLabel(20, 95, 84, 19, "Email:", false, GUIEditor.window[2])
	guiSetFont(GUIEditor.label[4], "default-blod-small")
	GUIEditor.edit[4] = guiCreateEdit(94, 85, 156, 36, "", false, GUIEditor.window[2])
	guiEditSetMaxLength(GUIEditor.edit[4], 100)
	GUIEditor.label[5] = guiCreateLabel(20, 146, 84, 19, "Password:", false, GUIEditor.window[2])
	guiSetFont(GUIEditor.label[5], "default-blod-small")
	GUIEditor.edit[5] = guiCreateEdit(94, 136, 156, 36, "", false, GUIEditor.window[2])
	guiEditSetMaxLength(GUIEditor.edit[5], 50)
	guiEditSetMasked(GUIEditor.edit[5], true)
	GUIEditor.button[3] = guiCreateButton(94, 185, 156, 36, "Register", false, GUIEditor.window[2])
	GUIEditor.button[4] = guiCreateButton(94, 231, 156, 36, "Back", false, GUIEditor.window[2])
	addEventHandler("onClientGUIClick", GUIEditor.button[3], onClientSubmitRegister, false)
	addEventHandler("onClientGUIClick", GUIEditor.button[4], hideRegisterWindow, false)
	guiSetVisible(GUIEditor.window[2], true)
end

function reousceStart()
    createLoginWindow()
	if GUIEditor.window[1] ~= nil then
	   guiSetVisible(GUIEditor.window[1], true)
	else
	   exports["notification-system"]:sendClientMessage("ERROR: An ERROR has been occupied!", 255, 0, 0)
	end
	showCursor(true)
	guiSetInputEnabled(true)
	guiSetInputMode("no_binds")
end	

local oldXmlFileName = "settings.xml"
local migratedSettingsFile = "@migratedsettings.empty"
local xmlFileName = "@settings.xml"
function loadSavedData(parameter, default)
	if not fileExists(migratedSettingsFile) then
		if not fileExists(xmlFileName) and fileExists(oldXmlFileName) then
			fileRename(oldXmlFileName, xmlFileName)
		end
		fileClose(fileCreate(migratedSettingsFile))
	end
	local xmlRoot = xmlLoadFile( xmlFileName )
	if (xmlRoot) then
		local xmlNode = xmlFindChild(xmlRoot, parameter, 0)
		if (xmlNode) then
			return xmlNodeGetValue(xmlNode)
		end
	end
	return default or false
end

function appendSavedData(parameter, value)
	setElementData(getLocalPlayer(), parameter, value, false)
	local xmlFile = xmlLoadFile ( xmlFileName )
	if not (xmlFile) then
		xmlFile = xmlCreateFile( xmlFileName, "login" )
	end
	
	local xmlNode = xmlFindChild (xmlFile, parameter, 0)
	if not (xmlNode) then
		xmlNode = xmlCreateChild(xmlFile, parameter)
	end
	xmlNodeSetValue ( xmlNode, value )
	xmlSaveFile(xmlFile)
	xmlUnloadFile(xmlFile)
end

function checkDetails()
    local username = guiGetText(GUIEditor.edit[1])
	local password = guiGetText(GUIEditor.edit[2])
	if guiCheckBoxGetSelected(GUIEditor.checkbox[1]) then
	   appendSavedData("username", tostring(username))
	   appendSavedData("password", tostring(password))
	else
	   appendSavedData("username", "")
	   appendSavedData("password", "")
	end
end

function onClientSubmitLogin(button, state)
    if button == "left" and state == "up" then
	   local username = guiGetText(GUIEditor.edit[1])
	   local password = guiGetText(GUIEditor.edit[2])
	   if username ~= "" and password ~= "" then
	      if username == "" then 
		      exports["notification-system"]:sendClientMessage("ERROR:  You need enter username!" , 255, 0, 0) 
	      elseif password == "" then 
		      exports["notification-system"]:sendClientMessage("ERROR: You need  enter a password!", 255, 0, 0) 
		  end
		  triggerServerEvent("submitLogin", root, localPlayer, username, password)
	   else
	      exports["notification-system"]:sendClientMessage("ERROR: You need enter username and password!", 255, 0, 0)
	   end
	end
end

function onClientSubmitRegister(button, state)
    if button == "left" and state == "up" then
	   local username = guiGetText(GUIEditor.edit[3])
	   local email = guiGetText(GUIEditor.edit[4])
	   local password = guiGetText(GUIEditor.edit[5])
	   if username ~= "" and email ~= "" and password ~= "" then
	      if username == "" then
		     exports["notification-system"]:sendClientMessage("ERROR: You need enter username!", 255, 0, 0)
		  elseif email == "" then
		     exports["notification-system"]:sendClientMessage("ERROR You need enter a email!", 255, 0, 0)
		  elseif password == "" then
		     exports["notification-system"]:sendClientMessage("ERROR: You need  enter a password!", 255, 0, 0) 
		  end
		  if string.len(username) < 3  then
		     exports["notification-system"]:sendClientMessage("ERROR: Username is too short you need 3 characters long!" , 255, 0, 0)
		  end
		  if string.len(password) < 6 then
		     exports["notification-system"]:sendClientMessage("ERROR: Password is too short you need 6 characters long!", 255, 0, 0)
		  end
		  if not string.find(email, "@") then 
		     exports["notification-system"]:sendClientMessage("ERROR: Invaild email!", 255, 0, 0)
		  end
		  triggerServerEvent("submitRegister", root , localPlayer, username, email, password)
	   else
	      exports["notification-system"]:sendClientMessage("ERROR You need enter username , email and password!", 255, 0, 0)
	   end
	end
end

function onClientSumbitSaveInformaiton(button, state)
    if button == "left" and state == "up" then
	   if guiCheckBoxGetSelected(GUIEditor.checkbox[1]) then
	      guiCheckBoxSetSelected(GUIEditor.checkbox[1], true)
		  guiSetText(GUIEditor.edit[1], tostring(loadSavedData("username", "")))
		  guiSetText(GUIEditor.edit[2], tostring(loadSavedData("password", "")))
	   else
	      guiCheckBoxSetSelected(GUIEditor.checkbox[1], false)
		  guiSetText(GUIEditor.edit[1], "")
		  guiSetText(GUIEditor.edit[2], "")
	   end
	end
end

function hideLoginWindow()
    guiSetVisible(GUIEditor.window[1], false)
	showChat(true)
	setPlayerHudComponentVisible("all", true)
	guiSetInputEnabled(false)
	showCursor(false)
end

function hideRegisterWindow()
    createLoginWindow()
	guiSetVisible(GUIEditor.window[2], false)
end
addEvent("hideLoginWindow", true)
addEvent("hideRegisterWindow", true)
addEventHandler("hideLoginWindow", root, hideLoginWindow)
addEventHandler("hideRegisterWindow", root, hideRegisterWindow)
addEventHandler("onClientResourceStart", resourceRoot, reousceStart)

i register a account for example with abc username  123456 passowrd and when try login say it incorrect why?

Edited by Dimos7
Link to comment
15 minutes ago, Dimos7 said:

that is on register i change it but that has nothing to do with login

re-register your account 

+ change this ( in login handler code ) ( line 6 )

  1. local query = dbQuery(connection, "SELECT * FROM accounts WHERE username and password =?", tostring(username), tostring(hashedPassword))

to this

  1. local query = dbQuery(connection, "SELECT * FROM accounts WHERE username = ? AND password = ?", tostring(username), tostring(hashedPassword))
Link to comment

same

local connection = exports["mysql"]:getConnection()

function loginHandler(thePlayer, username, password)
   local account = getAccount(username)
   local hashedPassword = getAccountData(account, "hashedPassword")
   local query = dbQuery(connection, "SELECT * FROM accounts WHERE username  =?and password =?", tostring(username), tostring(hashedPassword))
   local result, num_affected_row = dbPoll(query, -1)
   if account ~= nil and passwordVerify(password, hashedPassword, function(isVaild) if not isVaild then exports["notification-system"]:sendClientMessage("ERROR :Password is invaild!", thePlayer, 255, 0, 0) end end) and num_affected_row > 1 then
      if (logIn(thePlayer, account, hashedPassword) == true ) then
	       triggerClientEvent(thePlayer, "hideLoginWindow", root)
		   setElementData(thePlayer, "account:id", result.id)
		   dbExec(connection, "UPDATE accounts SET lastlogin=? WHERE username=?", "NOW()", tostring(username))
		   exports["notification-system"]:sendClientMessage("INFO: You successfuly logged in!", thePlayer ,0, 255, 0)
	  else
	     exports["notification-system"]:sendClientMessage("ERROR: That account is already logged in!", thePlayer, 255, 0, 0) 
	  end
	else
	  exports["notification-system"]:sendClientMessage("ERROR: Username or passwrod is invailed!", thePlayer, 255, 0, 0)
   end
end

function reigsterHandler(thePlayer, username, email, password)
    local account = getAccount(username)
	local query = dbQuery(connection, "SELECT * FROM accounts WHERE username =?",tostring(username))
	local result, num_affected_row = dbPoll(query, -1)
	if account ~= nil and num_affected_row > 1 then
	   exports["notification-system"]:sendClientMessage("ERROR: That username is already exist!", thePlayer, 255, 0, 0)
	else
	    passwordHash(password, "bcrypt", {}, function(hashedPassword)
		    local account = addAccount(username,hashedPassword)
		    setAccountData(account, "hashedPassword", hashedPassword)
		    local ip = getPlayerIP(thePlayer)
		    local serial = getPlayerSerial(thePlayer)
		    local r = getRealTime()
		    local regDate = ("%04d-%02d-%02d %02d:%02d:%02d"):format(r.year+1900, r.month+1, r.monthday, r.hour, r.minute, r.second)
		    dbExec(connection, "INSERT INTO accounts (username, password ,email, ip, serial, registerdate) VALUES(?, ?, ?, ?, ?, ?)", tostring(username), tostring(hashedPassword), tostring(email), tostring(ip), tostring(serial), tostring(regDate)) 
		    triggerClientEvent(thePlayer, "hideRegisterWindow", root)
		    exports["notification-system"]:sendClientMessage("INFO: You successfuly register!", thePlayer, 0, 255, 0)
		end)
	end
end
addEvent("submitLogin", true)
addEvent("submitRegister", true)
addEventHandler("submitLogin", root, loginHandler)
addEventHandler("submitRegister", root, reigsterHandler)

 

Link to comment

=?and

same , huh ?

local connection = exports["mysql"]:getConnection()
function loginHandler(thePlayer, username, password)
   local account = getAccount(username)
   local hashedPassword = getAccountData(account, "hashedPassword")
   local query = dbQuery(connection, "SELECT * FROM accounts WHERE username = ? AND password = ?", tostring(username), tostring(hashedPassword))
   local result, num_affected_row = dbPoll(query, -1)
   if account ~= nil and passwordVerify(password, hashedPassword, function(isVaild) if not isVaild then exports["notification-system"]:sendClientMessage("ERROR :Password is invaild!", thePlayer, 255, 0, 0) end end) and num_affected_row > 1 then
      if (logIn(thePlayer, account, hashedPassword) == true ) then
	       triggerClientEvent(thePlayer, "hideLoginWindow", root)
		   setElementData(thePlayer, "account:id", result.id)
		   dbExec(connection, "UPDATE accounts SET lastlogin=? WHERE username=?", "NOW()", tostring(username))
		   exports["notification-system"]:sendClientMessage("INFO: You successfuly logged in!", thePlayer ,0, 255, 0)
	  else
	     exports["notification-system"]:sendClientMessage("ERROR: That account is already logged in!", thePlayer, 255, 0, 0) 
	  end
	else
	  exports["notification-system"]:sendClientMessage("ERROR: Username or passwrod is invailed!", thePlayer, 255, 0, 0)
   end
end
function reigsterHandler(thePlayer, username, email, password)
    local account = getAccount(username)
	local query = dbQuery(connection, "SELECT * FROM accounts WHERE username =?",tostring(username))
	local result, num_affected_row = dbPoll(query, -1)
	if account ~= nil and num_affected_row > 1 then
	   exports["notification-system"]:sendClientMessage("ERROR: That username is already exist!", thePlayer, 255, 0, 0)
	else
	    passwordHash(password, "bcrypt", {}, function(hashedPassword)
		    local account = addAccount(username,hashedPassword)
		    setAccountData(account, "hashedPassword", hashedPassword)
		    local ip = getPlayerIP(thePlayer)
		    local serial = getPlayerSerial(thePlayer)
		    local r = getRealTime()
		    local regDate = ("%04d-%02d-%02d %02d:%02d:%02d"):format(r.year+1900, r.month+1, r.monthday, r.hour, r.minute, r.second)
		    dbExec(connection, "INSERT INTO accounts (username, password ,email, ip, serial, registerdate) VALUES(?, ?, ?, ?, ?, ?)", tostring(username), tostring(hashedPassword), tostring(email), tostring(ip), tostring(serial), tostring(regDate)) 
		    triggerClientEvent(thePlayer, "hideRegisterWindow", root)
		    exports["notification-system"]:sendClientMessage("INFO: You successfuly register!", thePlayer, 0, 255, 0)
		end)
	end
end
addEvent("submitLogin", true)
addEvent("submitRegister", true)
addEventHandler("submitLogin", root, loginHandler)
addEventHandler("submitRegister", root, reigsterHandler)

 

Link to comment

Edit mine.

 

 

local DisabledCharacters = "[\\/\\\\\"\\]\\[\\'\\-{}^$*+?,;:!£%&()_=@#~,<.>?]"
addEvent("sourceRequest[Register]", true)
addEventHandler("sourceRequest[Register]", root, function(username, password)
	if (client) and (client == source) then
		local playerSerial = getPlayerSerial(source)
		if username and #username > 3 and password and #password > 3 then
			if not pregFind(username, DisabledCharacters) and not pregFind(password, DisabledCharacters)  then
        		dbQuery(function(queryHandler, source)
					local result, rows = dbPoll(queryHandler, -1)
					for _, data in ipairs(result) do
						if data["Username"] == username then
							outputChatBox("This username already in use")
							return
						end
						if data["Serial"] == playerSerial then

							outputChatBox("You already have an account")
							return
						end
					end

					-- YOUR DBEXEC HERE
					outputChatBox("Successfully registered!")
	  		    	

				end, {source, username, password}, mysql:getConnection(), "SELECT * FROM accounts")
			else

				outputChatBox("You can't use special characters!")
			end
 		else
 			outputChatBox("Username / password must be 3 character")

		end
	end
end)

 

Link to comment
  • 2 weeks later...
local connection = exports["mysql"]:getConnection()
function loginHandler(username, password)
   if (client) and (client == source) then
      local account = getAccount(username)
      local hashedPass = getAccountData(account, "hashed_Password")
      dbQuery(function(queryHandler)
      local result, num_affected_row = dbPoll(queryHandler, 0)
        if account ~= nil and passwordVerify(password, hashedPass, function(isVaild) if not isVaild then exports["notification-system"]:sendClientMessage("ERROR :Password is invaild!", source, 255, 0, 0) end end) and num_affected_row > 1 then
            if (logIn(source, account, hashedPass) == true ) then
	            triggerClientEvent(source, "hideLoginWindow", root)
		        setElementData(source, "account:id", result.id)
		        dbExec(connection, "UPDATE accounts SET lastlogin=? WHERE username=?", "NOW()", tostring(username))
		        exports["notification-system"]:sendClientMessage("INFO: You successfuly logged in!", source ,0, 255, 0)
	        else
	           exports["notification-system"]:sendClientMessage("ERROR: That account is already logged in!", source, 255, 0, 0) 
	        end
	    else
	        exports["notification-system"]:sendClientMessage("ERROR: Username or passwrod is invailed!", source, 255, 0, 0)
		end	
      end, connection, "SELECT * FROM accounts WHERE username = ? AND password = ?", tostring(username), tostring(hashedPass))
   end
end

function reigsterHandler(username, email, password)
    if (client) and  (client == source) then
       local account = getAccount(username)
	   dbQuery(function(queryHandler)
	        local result, num_affected_row = dbPoll(queryHandler, 0)
	        if account ~= nil and num_affected_row > 1 then
	            exports["notification-system"]:sendClientMessage("ERROR: That username is already exist!", source, 255, 0, 0)
	        else
	            passwordHash(password, "bcrypt", {}, function(hashedPassword)
		            local account = addAccount(username,hashedPassword)
		            setAccountData(account, "hashed_Password", hashedPassword)
		            local ip = getPlayerIP(source)
		            local serial = getPlayerSerial(source)
		            local r = getRealTime()
		            local regDate = ("%04d-%02d-%02d %02d:%02d:%02d"):format(r.year+1900, r.month+1, r.monthday, r.hour, r.minute, r.second)
		            dbExec(connection, "INSERT INTO accounts (username, password ,email, ip, serial, registerdate) VALUES(?, ?, ?, ?, ?, ?)", tostring(username), tostring(hashedPassword), tostring(email), tostring(ip), tostring(serial), tostring(regDate)) 
		            triggerClientEvent(source, "hideRegisterWindow", root)
		            exports["notification-system"]:sendClientMessage("INFO: You successfuly register!", source, 0, 255, 0)
	 	        end)
	        end
	   end, ,connection, "SELECT username FROM accounts WHERE username =?",tostring(username))
	end
end
addEvent("submitLogin", true)
addEvent("submitRegister", true)
addEventHandler("submitLogin", root, loginHandler)
addEventHandler("submitRegister", root, reigsterHandler)

after change it a bit that appear unexpected symbol near , line 44

Edited by Dimos7
Link to comment
  • 3 weeks later...
local connection = exports["mysql"]:getConnection()
function loginHandler(client,username, password)
   if (client) and (client == source) then
      local account = getAccount(username)
      local hashedPass = getAccountData(account, "hashed_Password")
      dbQuery(function(queryHandler)
      local result, num_affected_row = dbPoll(queryHandler, 0)
        if account ~= nil and passwordVerify(password, hashedPass, function(isVaild) if not isVaild then exports["notification-system"]:sendClientMessage("ERROR :Password is invaild!", source, 255, 0, 0) end end) and num_affected_row > 1 then
            if (logIn(source, account, hashedPass) == true ) then
	            triggerClientEvent(source, "hideLoginWindow", root)
		        setElementData(source, "account:id", result.id)
		        dbExec(connection, "UPDATE accounts SET lastlogin=? WHERE username=?", "NOW()", tostring(username))
		        exports["notification-system"]:sendClientMessage("INFO: You successfuly logged in!", source ,0, 255, 0)
	        else
	           exports["notification-system"]:sendClientMessage("ERROR: That account is already logged in!", source, 255, 0, 0) 
	        end
	    else
	        exports["notification-system"]:sendClientMessage("ERROR: Username or passwrod is invailed!", source, 255, 0, 0)
		end	
      end, connection, "SELECT * FROM accounts WHERE username = ? AND password = ?", tostring(username), tostring(hashedPass))
   end
end

function reigsterHandler(client,username, email, password)
    if (client) and  (client == source) then
       local account = getAccount(username)
	   dbQuery(function(queryHandler)
	        local result, num_affected_row = dbPoll(queryHandler, 0)
	        if account ~= nil and num_affected_row > 1 then
	            exports["notification-system"]:sendClientMessage("ERROR: That username is already exist!", source, 255, 0, 0)
	        else
	            passwordHash(password, "bcrypt", {}, function(hashedPassword)
		            local account = addAccount(username,hashedPassword)
		            setAccountData(account, "hashed_Password", hashedPassword)
		            local ip = getPlayerIP(source)
		            local serial = getPlayerSerial(source)
		            local r = getRealTime()
		            local regDate = ("%04d-%02d-%02d %02d:%02d:%02d"):format(r.year+1900, r.month+1, r.monthday, r.hour, r.minute, r.second)
		            dbExec(connection, "INSERT INTO accounts (username, password ,email, ip, serial, registerdate) VALUES(?, ?, ?, ?, ?, ?)", tostring(username), tostring(hashedPassword), tostring(email), tostring(ip), tostring(serial), tostring(regDate)) 
		            triggerClientEvent(source, "hideRegisterWindow", root)
		            exports["notification-system"]:sendClientMessage("INFO: You successfuly register!", source, 0, 255, 0)
	 	        end)
	        end
	   end,connection, "SELECT username FROM accounts WHERE username =?",tostring(username))
	end
end
addEvent("submitLogin", true)
addEvent("submitRegister", true)
addEventHandler("submitLogin", root, loginHandler)
addEventHandler("submitRegister", root, reigsterHandler)
local sw, sh = guiGetScreenSize()
showChat(false)
setPlayerHudComponentVisible("all", false)

local GUIEditor = {
    window = {},
	label = {},
	edit = {},
	memo = {},
	checkbox = {},
	button = {},
}


function createLoginWindow()
    GUIEditor.window[1] = guiCreateWindow((sw-662)/2, (sh-285)/2, 662, 285, "Login", false)
	guiWindowSetSizable(GUIEditor.window[1], false)
	guiWindowSetMovable(GUIEditor.window[1], false)
	GUIEditor.label[1] = guiCreateLabel(20, 49, 84, 19, "Username:", false, GUIEditor.window[1])
	guiSetFont(GUIEditor.label[1], "default-blod-small")
	GUIEditor.edit[1] = guiCreateEdit(94, 39, 156, 36, "", false, GUIEditor.window[1])
	guiEditSetMaxLength(GUIEditor.edit[1], 20)
	GUIEditor.label[2] = guiCreateLabel(20, 95, 84, 19, "Password:", false, GUIEditor.window[1])
	guiSetFont(GUIEditor.label[2], "default-blod-small")
	GUIEditor.edit[2] = guiCreateEdit(94, 85, 156, 36, "", false, GUIEditor.window[1])
	guiEditSetMaxLength(GUIEditor.edit[2], 50)
	guiEditSetMasked(GUIEditor.edit[2], true)
	guiSetText(GUIEditor.edit[1], tostring(loadSavedData("username", "")))
	guiSetText(GUIEditor.edit[2], tostring(loadSavedData("password", "")))
	GUIEditor.button[1] = guiCreateButton(94, 185, 156, 36, "Login", false, GUIEditor.window[1])
	GUIEditor.button[2] = guiCreateButton(94, 231, 156, 36, "Register", false, GUIEditor.window[1])
	GUIEditor.checkbox[1] = guiCreateCheckBox(94, 136, 149, 15, "Rememeber information", false, false, GUIEditor.window[1])
	GUIEditor.memo[1] = guiCreateMemo(296, 39, 351, 231, "Hey, you there!\n\nWelcome to the great experience of Countyside Roleplay.\n\nWe hope to enjoy your time here", false, GUIEditor.window[1])
	guiMemoSetReadOnly(GUIEditor.memo[1], true)
	addEventHandler("onClientGUIClick", GUIEditor.button[1], onClientSubmitLogin, false)
	addEventHandler("onClientGUIClick", GUIEditor.button[2], createRegisterWindow, false)
end

function createRegisterWindow()
    guiSetVisible(GUIEditor.window[1], false)
    GUIEditor.window[2] = guiCreateWindow((sw-311)/2, (sh-285)/2, 311, 285, "Registration", false)
	guiWindowSetMovable(GUIEditor.window[2], false)
	guiWindowSetSizable(GUIEditor.window[2], false)
	GUIEditor.label[3] = guiCreateLabel(20, 49, 84, 19, "Username:", false, GUIEditor.window[2])
	guiSetFont(GUIEditor.label[3], "default-blod-small")
	GUIEditor.edit[3] = guiCreateEdit(94, 39, 156, 36, "", false, GUIEditor.window[2])
	guiEditSetMaxLength(GUIEditor.edit[3], 20)
	GUIEditor.label[4] = guiCreateLabel(20, 95, 84, 19, "Email:", false, GUIEditor.window[2])
	guiSetFont(GUIEditor.label[4], "default-blod-small")
	GUIEditor.edit[4] = guiCreateEdit(94, 85, 156, 36, "", false, GUIEditor.window[2])
	guiEditSetMaxLength(GUIEditor.edit[4], 100)
	GUIEditor.label[5] = guiCreateLabel(20, 146, 84, 19, "Password:", false, GUIEditor.window[2])
	guiSetFont(GUIEditor.label[5], "default-blod-small")
	GUIEditor.edit[5] = guiCreateEdit(94, 136, 156, 36, "", false, GUIEditor.window[2])
	guiEditSetMaxLength(GUIEditor.edit[5], 50)
	guiEditSetMasked(GUIEditor.edit[5], true)
	GUIEditor.button[3] = guiCreateButton(94, 185, 156, 36, "Register", false, GUIEditor.window[2])
	GUIEditor.button[4] = guiCreateButton(94, 231, 156, 36, "Back", false, GUIEditor.window[2])
	addEventHandler("onClientGUIClick", GUIEditor.button[3], onClientSubmitRegister, false)
	addEventHandler("onClientGUIClick", GUIEditor.button[4], hideRegisterWindow, false)
	guiSetVisible(GUIEditor.window[2], true)
end

function reousceStart()
    createLoginWindow()
	if GUIEditor.window[1] ~= nil then
	   guiSetVisible(GUIEditor.window[1], true)
	else
	   exports["notification-system"]:sendClientMessage("ERROR: An ERROR has been occupied!", 255, 0, 0)
	end
	showCursor(true)
	guiSetInputEnabled(true)
	guiSetInputMode("no_binds")
end	

local oldXmlFileName = "settings.xml"
local migratedSettingsFile = "@migratedsettings.empty"
local xmlFileName = "@settings.xml"
function loadSavedData(parameter, default)
	if not fileExists(migratedSettingsFile) then
		if not fileExists(xmlFileName) and fileExists(oldXmlFileName) then
			fileRename(oldXmlFileName, xmlFileName)
		end
		fileClose(fileCreate(migratedSettingsFile))
	end
	local xmlRoot = xmlLoadFile( xmlFileName )
	if (xmlRoot) then
		local xmlNode = xmlFindChild(xmlRoot, parameter, 0)
		if (xmlNode) then
			return xmlNodeGetValue(xmlNode)
		end
	end
	return default or false
end

function appendSavedData(parameter, value)
	setElementData(localPlayer, parameter, value, false)
	local xmlFile = xmlLoadFile ( xmlFileName )
	if not (xmlFile) then
		xmlFile = xmlCreateFile( xmlFileName, "login" )
	end
	
	local xmlNode = xmlFindChild (xmlFile, parameter, 0)
	if not (xmlNode) then
		xmlNode = xmlCreateChild(xmlFile, parameter)
	end
	xmlNodeSetValue ( xmlNode, value )
	xmlSaveFile(xmlFile)
	xmlUnloadFile(xmlFile)
end


function onClientSubmitLogin(button, state)
    if button == "left" and state == "up" then
	   local username = guiGetText(GUIEditor.edit[1])
	   local password = guiGetText(GUIEditor.edit[2])
	   if username ~= "" and password ~= "" then
	        if username == "" then 
		        exports["notification-system"]:sendClientMessage("ERROR:  You need enter username!" , 255, 0, 0) 
	        elseif password == "" then 
		        exports["notification-system"]:sendClientMessage("ERROR: You need  enter a password!", 255, 0, 0) 
		    end
		    if guiCheckBoxGetSelected(GUIEditor.checkbox[1]) then
	           appendSavedData("username", tostring(username))
	           appendSavedData("password", tostring(password))
	        else
	           appendSavedData("username", "")
	           appendSavedData("password", "")
		    end
	       triggerServerEvent("submitLogin", resourceRoot,  username, password)
		else
		   exports["notification-system"]:sendClientMessage("ERROR: You need enter username and password!!", 255, 0, 0)
	   end
	end
end

function onClientSubmitRegister(button, state)
    if button == "left" and state == "up" then
	   local username = guiGetText(GUIEditor.edit[3])
	   local email = guiGetText(GUIEditor.edit[4])
	   local password = guiGetText(GUIEditor.edit[5])
	   if username ~="" and email ~= "" and password ~= "" then
	        if username == "" then
		        exports["notification-system"]:sendClientMessage("ERROR: You need enter username!", 255, 0, 0)
		    elseif email == "" then
		        exports["notification-system"]:sendClientMessage("ERROR You need enter a email!", 255, 0, 0)
		    elseif password == "" then
		        exports["notification-system"]:sendClientMessage("ERROR: You need  enter a password!", 255, 0, 0) 
		    end
		    if string.len(username) < 3  then
		        exports["notification-system"]:sendClientMessage("ERROR: Username is too short you need 3 characters long!" , 255, 0, 0)
		    end
		    if string.len(password) < 6 then
		       exports["notification-system"]:sendClientMessage("ERROR: Password is too short you need 6 characters long!", 255, 0, 0)
		    end
		    if not string.find(email, "@") then 
		        exports["notification-system"]:sendClientMessage("ERROR: Invaild email!", 255, 0, 0)
		    end
	     triggerServerEvent("submitRegister", resourceRoot, username, email, password)
		else
		  exports["notification-system"]:sendClientMessage("ERROR: You need enter username, email and password!!", 255, 0, 0)
		end
	end
end




function hideLoginWindow()
    guiSetVisible(GUIEditor.window[1], false)
	showChat(true)
	setPlayerHudComponentVisible("all", true)
	guiSetInputEnabled(false)
	showCursor(false)
end

function hideRegisterWindow()
    createLoginWindow()
	guiSetVisible(GUIEditor.window[2], false)
end
addEvent("hideLoginWindow", true)
addEvent("hideRegisterWindow", true)
addEventHandler("hideLoginWindow", root, hideLoginWindow)
addEventHandler("hideRegisterWindow", root, hideRegisterWindow)
addEventHandler("onClientResourceStart", resourceRoot, reousceStart)

Nothing work nether login or register no error or warnings

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...