Jump to content

[HELP] db query returns nil, pls help me


p1kas

Recommended Posts

there is a function to login to account:

function login(username, password)
    local player = client
    if not (does_account_exist(username)) then
  		create_error_message_dialog("Аккаунта с таким логином не существует!")
    	return;
    else
	    local handle = dbQuery(function (handle)
	        --callback for the query selecting the user by username
	        local results = dbPoll(handle, -1)
	        if (#results == 0) then
	        	outputChatBox("Login Failed!")
	            --triggerClientEvent(player, "loginFailed")
	            return
	        end
	        passwordVerify(password, results[1].password, {}, function(matches)
	            -- callback function for the password verify
	            if (matches) then
	                -- Do anything you wish with the database result to log the player in with the rest of your scripts
	                outputChatBox("Login Success!")
	                --triggerClientEvent(player, "loginSuccess")
	            else
	            	outputChatBox("Login Failed!")
	                --triggerClientEvent(player, "loginFailed")
	            end

	        if (rememberMe) then
		        local token = generateRandomToken()
		        dbExec(function() 
		            --triggerClientEvent(player, "loginSuccess", token)
		        end,mysqlHandle, "INSERT INTO `access_tokens` (`user_id`, `token`) VALUES ('"..tostring(results[1].id).."', '"..tostring(token).."')")
	    	end
	        end)

	    end, mysqlHandle, "SELECT * FROM `users` WHERE username = '"..username.."'")
	end
end

but when you call passwordVerify(), the results table for the username returns the correct username, and for the password it returns nil, how to fix this (if necessary, the password is encoded in the database)

 

I also wanted to ask, there is a button in the form of a rectangle:

local buttonAuth_round = dgs:dgsCreateRoundRect(30,false,tocolor(62,153,247))
local buttonAuth = dgs:dgsCreateImage(182,0,164,56,buttonAuth_round,false,loginButtons,tocolor(62,153,247))
local buttonAuth_text = dgs:dgsCreateLabel(57,18,50,20,'Войти', false, buttonAuth)

There is an event handler:

addEventHandler("onDgsMouseClick", buttonAuth, function()
	local login, password = dgs:dgsGetText(inputLogin_edit), dgs:dgsGetText(inputPass_edit)
	if not string.find(login, "%S") then return end
	if not string.find(password, "%S") then return end
	triggerServerEvent("playerLogin", getLocalPlayer(), login, password)
end, false)

But when you click on a button or any other dgs element (which is being processed) - it is triggered two times (simultaneously), how can this be fixed?

Link to comment
  • Moderators
On 14/08/2020 at 23:28, p1kas said:

but when you call passwordVerify(), the results table for the username returns the correct username, and for the password it returns nil, how to fix this (if necessary, the password is encoded in the database)

 

This:

results[1].password

returns nil?

 

First take a look in what is inside (this in most of the cases gives you the solution right away):

inspect("Input password:", password, "\n Database results:", results)

 

 



 

https://wiki.multitheftauto.com/wiki/DbExec

dbExec doesn't support callback functions. It returns true when the connection is successful. This does not give you any information if the value is correctly set.

		        dbExec(function() 
		            --triggerClientEvent(player, "loginSuccess", token)
		        end,

 

See here how to do that correctly (for insert):

https://wiki.multitheftauto.com/wiki/DbPoll

 

 

 

 

Edited by IIYAMA
  • Like 1
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...