Quebec Posted August 23, 2021 Share Posted August 23, 2021 I'm coming back with another question, I've been struggling for like 3 hours with this one. I'm trying to get an error message when I press the login button if username or password is incorrect. Maybe it's a typo but I can't find it. Here's the code: Client Side loadstring(exports.dgs:dgsImportFunction())() local screenW, screenH = guiGetScreenSize() local camerapos1 = Vector3(1324.3000488281, 231.19999694824, 19.10000038147) local camerapos2 = Vector3(1473.5, 319.10000610352, 60) function triggerLogin() setCameraMatrix(camerapos1, camerapos2) guiSetInputEnabled(true) setTimer(function() fadeCamera(true) showCursor(true) showChat(true) setPlayerHudComponentVisible('all', false) loginMenu() end, 1500, 1) end addEventHandler('onClientResourceStart', resourceRoot, triggerLogin) function loginMenu() loginwindow = dgsCreateWindow((screenW - 317) / 2, (screenH - 371) / 2, 317, 371, "Bun venit pe server!", false, nil, nil, nil, nil, nil, nil, nil, true) dgsWindowSetMovable(loginwindow, false) dgsWindowSetSizable(loginwindow, false) loginedit1 = dgsCreateEdit(89, 65, 205, 33, "", false, loginwindow) loginedit2 = dgsCreateEdit(89, 122, 205, 33, "", false, loginwindow) dgsEditSetMasked(loginedit2, true) loginlabel1 = dgsCreateLabel(10, 68, 74, 30, "Username:", false, loginwindow) loginlabel2 = dgsCreateLabel(10, 125, 74, 30, "Password:", false, loginwindow) logintemplabel = dgsCreateLabel(20, 165, 281, 17, "", false, loginwindow) dgsLabelSetHorizontalAlign(logintemplabel, "center", true) loginbutonlogin = dgsCreateButton(0, 305, 159, 41, "Logheaza-te", false, loginwindow) loginbutonregister = dgsCreateButton(159, 305, 158.5, 41, "Inregistreaza-te", false, loginwindow) addEventHandler('onDgsMouseClick', loginbutonregister, function(button, state) if button == "left" and state == "down" then destroyElement(loginwindow) registerMenu() end end, false) addEventHandler('onDgsMouseClick', loginbutonlogin, function(button, state) if button == "left" and state == "down" then validateLoginData() end end, false) end function registerMenu() registerwindow = dgsCreateWindow((screenW - 317) / 2, (screenH - 371) / 2, 317, 371, "Bun venit pe server!", false, nil, nil, nil, nil, nil, nil, nil, true) dgsWindowSetMovable(registerwindow, false) dgsWindowSetSizable(registerwindow, false) registeredit1 = dgsCreateEdit(89, 65, 205, 33, "", false, registerwindow) registeredit2 = dgsCreateEdit(89, 122, 205, 33, "", false, registerwindow) registeredit3 = dgsCreateEdit(89, 180, 205, 33, "", false, registerwindow) dgsEditSetMasked(registeredit2, true) registerlabel1 = dgsCreateLabel(10, 68, 74, 30, "Username:", false, registerwindow) registerlabel2 = dgsCreateLabel(10, 125, 74, 30, "Password:", false, registerwindow) registerlabel3 = dgsCreateLabel(10, 183, 74, 30, "Email:", false, registerwindow) registerbutonlogin = dgsCreateButton(0, 305, 159, 41, "Logheaza-te", false, registerwindow) registerbutonregister = dgsCreateButton(159, 305, 158.5, 41, "Inregistreaza-te", false, registerwindow) registertemplabel = dgsCreateLabel(20, 223, 281, 17, "", false, registerwindow) dgsLabelSetHorizontalAlign(registertemplabel, "center", true) addEventHandler('onDgsMouseClick', registerbutonlogin, function(button, state) if button == "left" and state == "down" then destroyElement(registerwindow) loginMenu() end end, false) addEventHandler('onDgsMouseClick', registerbutonregister, function(button, state) if button == "left" and state == "down" then validateRegisterData() end end, false) end function validateRegisterData() local username = dgsGetText(registeredit1) local password = dgsGetText(registeredit2) local email = dgsGetText(registeredit3) if username == "" or password == "" or email == "" then dgsLabelSetColor(registertemplabel, 255, 100, 100, 255) dgsSetText(registertemplabel, "Completeaza toate spatiile!") return false end if string.len(username) < 3 then dgsLabelSetColor(registertemplabel, 255, 100, 100, 255) dgsSetText(registertemplabel, "Numele trebuie sa fie mai mare de 3 caractere!") return false elseif string.len(password) < 6 then dgsLabelSetColor(registertemplabel, 255, 100, 100, 255) dgsSetText(registertemplabel, "Parola trebuie sa fie mai mare de 6 caractere!") return false elseif email == "" then dgsLabelSetColor(registertemplabel, 255, 100, 100, 255) dgsSetText(registertemplabel, "Introduce un email!") return false end triggerServerEvent("account:register", localPlayer, localPlayer, username, password, email) end addEvent('account:alreadyname', true) addEventHandler('account:alreadyname', root, function() dgsLabelSetColor(registertemplabel, 255, 100, 100, 255) dgsSetText(registertemplabel, "Un cont cu acest nume exista deja!") end) addEvent('account:succesfulregister', true) addEventHandler('account:succesfulregister', root, function() dgsLabelSetColor(registertemplabel, 100, 255, 100, 255) dgsSetText(registertemplabel, "Contul a fost creat cu succes!") end) function validateLoginData() local username = dgsGetText(loginedit1) local password = dgsGetText(loginedit2) if username == "" or password == "" then dgsLabelSetColor(logintemplabel, 255, 100, 100, 255) dgsSetText(logintemplabel, "Completeaza toate spatiile!") return false end triggerServerEvent("account:login", localPlayer, localPlayer, username, password) end ---PROBLEM addEvent('account:noaccount', true) addEventHandler('account:noaccount', root, function() dgsLabelSetColor(logintemplabel, 255, 100, 100, 255) dgsSetText(logintemplabel, "Nu exista acest cont!") end) ---PROBLEM addEvent('account:succesfullogin', true) addEventHandler('account:succesfullogin', root, function() dgsLabelSetColor(logintemplabel, 100, 255, 100, 255) dgsSetText(logintemplabel, "Ai fost logat cu succes!") end) addEvent('account:switchcamera', true) addEventHandler('account:switchcamera', root, function() setTimer(function() destroyElement(loginwindow) guiSetInputEnabled(false) showCursor(false) showChat(true) setPlayerHudComponentVisible('all', true) setCameraTarget(localPlayer, localPlayer) end, 3000, 1) end) Server Side local db = exports.mysqlconnection:getConnection() addEvent("account:register", true) addEventHandler("account:register", root, function(thePlayer, username, password, email) if not username or not password then return false end local queryCheck = dbQuery(db, "SELECT * FROM `accounts` WHERE `username` = '" .. username .. "'") local result = dbPoll(queryCheck, -1) if #result > 0 then return triggerClientEvent(thePlayer, "account:alreadyname", thePlayer) end local playerIp = getPlayerIP(thePlayer) local playerSerial = getPlayerSerial(thePlayer) local passHash = passwordHash(password, "bcrypt", {}) dbExec(db, "INSERT INTO `accounts` (`username`, `password`, `ip`, `serial`, `email`) VALUES (?, ?, ?, ?, ?)", username, passHash, playerIp, playerSerial, email) triggerClientEvent(thePlayer, 'account:succesfulregister', thePlayer) end) addEvent("account:login", true) addEventHandler("account:login", root, function(thePlayer, username, password) if not username or not password then return false end local queryCheck = dbQuery(db, "SELECT * FROM `accounts` WHERE `username` = '" .. username .. "'") local result = dbPoll(queryCheck, -1) if result then for k, row in ipairs(result) do local hashedPassword = row["password"] if passwordVerify(password, hashedPassword) then triggerClientEvent(thePlayer, 'account:succesfullogin', thePlayer) setElementData(thePlayer, "account:id", row["id"]) setElementData(thePlayer, "account:username", row["username"]) setElementData(thePlayer, "account:email", row["email"]) setElementData(thePlayer, "account:ip", row["ip"]) setElementData(thePlayer, "account:serial", row["serial"]) setTimer(function() spawnPlayer(thePlayer, 1245.1999511719, 332.5, 19.200000762939) triggerClientEvent(thePlayer, 'account:switchcamera', thePlayer) end, 2900, 1) end end else --this is where I dont get anything in return (tried putting outputconsole of an error message but i dont get that either) return triggerClientEvent(thePlayer, 'account:noaccount', thePlayer) end end) Red area is where dgs label should appear. All other labels work. Link to comment
The_GTA Posted August 23, 2021 Share Posted August 23, 2021 Have you tried putting in a username that does not exist? Your code does only seem to send a client event if no such user exists. Link to comment
Quebec Posted August 23, 2021 Author Share Posted August 23, 2021 2 minutes ago, The_GTA said: Have you tried putting in a username that does not exist? Your code does only seem to send a client event if no such user exists. yes, that's what im testing it with. I want the code to trigger 'account:noaccount' when no such username is found. Link to comment
The_GTA Posted August 23, 2021 Share Posted August 23, 2021 6 minutes ago, Quebec said: yes, that's what im testing it with. I want the code to trigger 'account:noaccount' when no such username is found. Please take a look at the Wiki page of the dbPoll function. There it says that it does return a table of results if the poll was successful but it returns false or nil if an API or connection error occured. From the effects that you are seeing, the dbPoll request is successful but empty. Please try this code instead: addEvent("account:login", true) addEventHandler("account:login", root, function(thePlayer, username, password) if not username or not password then return false end local queryCheck = dbQuery(db, "SELECT * FROM `accounts` WHERE `username` = '" .. username .. "'") local result = dbPoll(queryCheck, -1) if result then for k,row in ipairs(result) do local hashedPassword = row["password"] if passwordVerify(password, hashedPassword) then triggerClientEvent(thePlayer, 'account:succesfullogin', thePlayer) setElementData(thePlayer, "account:id", row["id"]) setElementData(thePlayer, "account:username", row["username"]) setElementData(thePlayer, "account:email", row["email"]) setElementData(thePlayer, "account:ip", row["ip"]) setElementData(thePlayer, "account:serial", row["serial"]) setTimer(function() spawnPlayer(thePlayer, 1245.1999511719, 332.5, 19.200000762939) triggerClientEvent(thePlayer, 'account:switchcamera', thePlayer) end, 2900, 1) return; end end triggerClientEvent(thePlayer, 'account:noaccount', thePlayer) else print("database error"); --this is where I dont get anything in return (tried putting outputconsole of an error message but i dont get that either) end end) 1 Link to comment
Quebec Posted August 23, 2021 Author Share Posted August 23, 2021 5 minutes ago, The_GTA said: Please take a look at the Wiki page of the dbPoll function. There it says that it does return a table of results if the poll was successful but it returns false or nil if an API or connection error occured. From the effects that you are seeing, the dbPoll request is successful but empty. Please try this code instead: addEvent("account:login", true) addEventHandler("account:login", root, function(thePlayer, username, password) if not username or not password then return false end local queryCheck = dbQuery(db, "SELECT * FROM `accounts` WHERE `username` = '" .. username .. "'") local result = dbPoll(queryCheck, -1) if result then for k,row in ipairs(result) do local hashedPassword = row["password"] if passwordVerify(password, hashedPassword) then triggerClientEvent(thePlayer, 'account:succesfullogin', thePlayer) setElementData(thePlayer, "account:id", row["id"]) setElementData(thePlayer, "account:username", row["username"]) setElementData(thePlayer, "account:email", row["email"]) setElementData(thePlayer, "account:ip", row["ip"]) setElementData(thePlayer, "account:serial", row["serial"]) setTimer(function() spawnPlayer(thePlayer, 1245.1999511719, 332.5, 19.200000762939) triggerClientEvent(thePlayer, 'account:switchcamera', thePlayer) end, 2900, 1) return; end end triggerClientEvent(thePlayer, 'account:noaccount', thePlayer) else print("database error"); --this is where I dont get anything in return (tried putting outputconsole of an error message but i dont get that either) end end) This actually works! Thanks man! But I really want to understand the issue if you don't mind... so when I put a non existing username the "if result then" statement was true because i got an empty table and it ran the for loop? Link to comment
The_GTA Posted August 23, 2021 Share Posted August 23, 2021 Just now, Quebec said: This actually works! Thanks man! But I really want to understand the issue if you don't mind... so when I put a non existing username the "if result then" statement was true because i got an empty table and it ran the for loop? Yes. An empty database result is returned as an empty table. There is no connection error so dbPoll does not return false. You are using the API correctly, too (besides the -1 for dbPoll which is a freeze-hazard for your server). 1 Link to comment
Quebec Posted August 23, 2021 Author Share Posted August 23, 2021 1 minute ago, The_GTA said: Yes. An empty database result is returned as an empty table. There is no connection error so dbPoll does not return false. You are using the API correctly, too (besides the -1 for dbPoll which is a freeze-hazard for your server). -1 for dbPoll can cause freeze on the server? should I give an actual value in miliseconds rather than -1? Link to comment
The_GTA Posted August 23, 2021 Share Posted August 23, 2021 Just now, Quebec said: -1 for dbPoll can cause freeze on the server? should I give an actual value in miliseconds rather than -1? Yes. I recommend you to put a realistic value like 5000 milliseconds. If it takes longer than that then there is some serious trouble going on inside of your database server. 1 Link to comment
Quebec Posted August 23, 2021 Author Share Posted August 23, 2021 Just now, The_GTA said: Yes. I recommend you to put a realistic value like 5000 milliseconds. If it takes longer than that then there is some serious trouble going on inside of your database server. Alright, thank you for the tip! You've helped me a lot. :D 1 Link to comment
Rockyz Posted August 24, 2021 Share Posted August 24, 2021 12 hours ago, Quebec said: -1 for dbPoll can cause freeze on the server? should I give an actual value in miliseconds rather than -1? You could use callbacks to avoid that an example from the wiki: local dbConnection = dbConnect("sqlite", "sql.db") function onServerCallback(queryHandler, extraData) local queryResult = dbPoll(queryHandler, 0) print(extraData) if type(queryResult) == "table" then print("queryResult == table") if #queryResult > 0 then print("#queryResult > 0") else print("#queryResult == 0") end else print("queryResult ~= table") end end dbQuery(onServerCallback, {"Extra data :)"}, dbConnection, "SELECT * FROM `Players` WHERE `playerName` = ?", "playerName") 2 Link to comment
Quebec Posted August 24, 2021 Author Share Posted August 24, 2021 4 hours ago, RocKyz said: You could use callbacks to avoid that an example from the wiki: local dbConnection = dbConnect("sqlite", "sql.db") function onServerCallback(queryHandler, extraData) local queryResult = dbPoll(queryHandler, 0) print(extraData) if type(queryResult) == "table" then print("queryResult == table") if #queryResult > 0 then print("#queryResult > 0") else print("#queryResult == 0") end else print("queryResult ~= table") end end dbQuery(onServerCallback, {"Extra data :)"}, dbConnection, "SELECT * FROM `Players` WHERE `playerName` = ?", "playerName") Oh, thanks. Will keep that in mind! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now