roDaN Posted January 23, 2022 Share Posted January 23, 2022 (edited) Hi community, recently i started a project and i'm trying to create an api for login. I have spent so much time (also a lot of research and examples), i modified the scripts many times but i can't really figure out what's wrong. Here is the lua: function login.connectAPI(username, password, player) if not isElement(player) then return end local username, password = tostring(username), tostring(password) print("login.connectAPI: "..tostring(login.usedAccounts[username])) if login.isPlayerLoggedIn[player] then return triggerClientEvent(player, "notification:create", player, "Log in", "You are already logged in.") elseif login.activeAttempts[player] then return triggerClientEvent(player, "notification:create", player, "Log in", "Please wait some time before trying to logging again.") end if type(username) ~= "string" then return triggerClientEvent(player, "notification:create", player, "Log in", "Please enter your username.") end if #username <= 2 then return triggerClientEvent(player, "notification:create", player, "Log in", "Your username is too short.") end if login.usedAccounts[username] then return triggerClientEvent(player, "notification:create", player, "Log in", "This account is already being used.") end if type(password) ~= "string" then return triggerClientEvent(player, "notification:create", player, "Log in", "Please enter your password.") end if #password <= 2 then return triggerClientEvent(player, "notification:create", player, "Log in", "Your password is too short.") end --local queueName = username, password local POST = toJSON({username = username, password = password}) fetchRemote("api link", "", 1, 10000, login.APICallBack, POST, false, player, password) login.activeAttempts[player] = true end function login.APICallBack(responseData, errorNo, player, password) if errorNo == 0 then local response = fromJSON(responseData) if type(response) ~= "table" then --return outputDebugString("Failed to convert JSON", 1) outputDebugString(fromJSON(POST)) outputDebugString(tostring(responseData) end if response.error == 0 then -- Login success response.password = password local assign = login.assingUserdata(player, response) if assign then triggerClientEvent(player, "notification:create", player, "Log in", "Successfully logged in as "..response.username) else triggerClientEvent(player, "notification:create", player, "Log in", "Failed to login as "..response.username) end else if response.message == 1 then -- Account is not found triggerClientEvent(player, "notification:create", player, "Log in", "An account with this name is not found") elseif response.message == 2 then -- Wrong password triggerClientEvent("notification:create", player, "Log in", "Wrong password") else outputDebugString("Unknown error while logging in player "..getPlayerName(player)) triggerClientEvent(player, "notification:create", player, "Log in", "Failed to login") end end else outputDebugString("Failed to log player "..getPlayerName(player).." in, error no: "..errorNo, 0) end if login.activeAttempts[player] then login.activeAttempts[player] = nil end end And here the php: function getUserData(){ $input = Mta::getInput(); //$username = $input[0]['username']; //$password = $input[0]['password']; $array = json_decode($input, true); if (isset($array['username']) && $array['password']){ //$arr = json_decode($plm, TRUE); global $connection; //echo $arr; /* Sql Check */ $stmt = $connection->pdo->prepare("SELECT * FROM accounts WHERE username = :username and password = :password"); //$pass = sha1(hashKey . $password); $stmt->bindParam(":username", $array['username']); $stmt->bindParam(":password", $array['password']); $stmt->execute(); $result = $stmt->rowCount(); $account_data = $stmt->fetch(PDO::FETCH_ASSOC); /* Condition */ if ($result > 0) { //if ($arr["success"]) { # Successful Login //$er0 = array('error' => 0); /* SUCCESS */ //$err0 = json_encode('error' => 0); //$er0 = '{"error":"0"}'; $er0 = array('error' => 0); //echo json_encode($arr); //$err0 = json_decode($er0, true); Mta::doReturn($er0); //$responseData = array('username' => $account_data['username'], 'password' => $account_data['password']); //$resultt = json_encode($account_data); //echo $resultt; Mta::doReturn($account_data); //echo $responseData; //$table = mta::getInput(); //$text = $table[0]; //$text = $table[1]; //echo $text; //} }else{ $er1 = array('error' => 1); /* INCORRECT DATA */ $err1 = json_encode($er1); Mta::doReturn($err1); } } } Edited January 23, 2022 by roDaN Link to comment
The_GTA Posted January 23, 2022 Share Posted January 23, 2022 Hello roDaN, could you please explain your scripts a little bit more? You seem to be using a combination of MTA Lua scripting and web service technology (PHP, HTTP). This looks really complicated and I would like to know what software you are using, where you got the guidance from on how to create your script, whether the PHP portion does use any renowned libraries and whether you have gotten any response on the PHP side at all by now. Link to comment
roDaN Posted January 23, 2022 Author Share Posted January 23, 2022 16 minutes ago, The_GTA said: Hello roDaN, could you please explain your scripts a little bit more? You seem to be using a combination of MTA Lua scripting and web service technology (PHP, HTTP). This looks really complicated and I would like to know what software you are using, where you got the guidance from on how to create your script, whether the PHP portion does use any renowned libraries and whether you have gotten any response on the PHP side at all by now. Hi, thanks for your response. I've got the lua scripts from github, but the creator didn't shared the api part. (i already edited the lua scripts to work without the api but they are too complex and seems like doing the api will be the way to go) If i set a json string manually on php the mta server gets that response, but i need to create a condition in php to give back the user his data and there is where the problem comes in as i can't succeed into getting a response from mta server to php. Link to comment
The_GTA Posted January 23, 2022 Share Posted January 23, 2022 5 minutes ago, roDaN said: I've got the lua scripts from github, but the creator didn't shared the api part. I would still appreciate if you at least gave us the author's name or the link to where you got the code from. This would be helpful if other users also encounter similar issues. 4 hours ago, roDaN said: outputDebugString(tostring(responseData) There is a syntax error in this line. 11 minutes ago, roDaN said: but i need to create a condition in php to I hope you understand that it is difficult for the outside user to understand your system. You are sharing a small fraction of an apparently fully integrated system. What we are expecting from you is a precise description of what already works. Share the messages you are already receiving, the debug output you see, an example scenario that does work, an example scenario that does not work. Link to comment
roDaN Posted January 25, 2022 Author Share Posted January 25, 2022 (edited) So finally the problem is this errors on the php side which is resulting to a nil response recived by the mta server. [php7:notice] [pid 198719] [client 194.163.138.97:38658] PHP Notice: Trying to access array offset on value of type bool in /var/www/html/functions.php on line 19 [php7:notice] [pid 198719] [client 194.163.138.97:38658] PHP Notice: Trying to access array offset on value of type bool in /var/www/html/functions.php on line 20 The lines are the second and third from the code: $input = mta::getInput(); $username = $input[0]; $password = $input[1]; Is there any way to check if the connection between mta server and php server is made ? I can see in my access.log that mta server is making the request. Also the access.log: ip - - [25/Jan/2022:02:22:58 +0100] "POST login_check.php HTTP/1.1" 301 486 "-" "MTA:SA Server port 22004 - See https://mtasa.com/agent/" ip - - [25/Jan/2022:02:22:58 +0100] "GET login_check.php HTTP/1.1" 200 4872 "-" "MTA:SA Server port 22004 - See https://mtasa.com/agent/" Edited January 25, 2022 by roDaN more details Link to comment
roDaN Posted January 25, 2022 Author Share Posted January 25, 2022 Sorry for double post but i can't edit my post anymore. Problem solved, please T.C Solution: MTA SDK was configurated wrong. Link to comment
The_GTA Posted January 25, 2022 Share Posted January 25, 2022 (edited) I was not aware that MTA had an official PHP SDK, but go figure. multitheftauto/mtasa-php-sdk: The official PHP SDK for the Multi Theft Auto Web Interface. (github.com) Sorry for not being able to support you in the way you wanted to, but you cannot expect volunteers like us to do this professionally especially with secrecy involved. I am glad that you figured it out yourself. Edited January 25, 2022 by The_GTA Link to comment
FernandoMTA Posted January 25, 2022 Share Posted January 25, 2022 8 hours ago, roDaN said: Sorry for double post but i can't edit my post anymore. Problem solved, please T.C Solution: MTA SDK was configurated wrong. Does the MTA sdk need to be fixed or is the code itself (GitHub repo) good? Link to comment
roDaN Posted January 25, 2022 Author Share Posted January 25, 2022 @The_GTA is was not about keep it secret, but in all exemples i saw on the mtasa forums what i have shared seemed to be all it was needed. Thanks anyway for your support, i appreciate it. @FernandoMTA The MTA SDK itself it's good, as i said the error was on my side because i configurated it wrong. 1 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