[SOLVED] I have some JS code that is just a password and i want it to get the password from a Lua file

Hello there, I want some serious help, I have some JavaScript code that is a password for an HTML UI, so I want it to extract the password from Lua

Here is the JS Code

$(function () {
    function display(bool) {
        if (bool) {
            $("#container").show();
        } else {
            $("#container").hide();
        }
    }

    display(false)

    window.addEventListener('message', function(event) {
        var item = event.data;
        if (item.type === "ui") {
            if (item.status == true) {
                display(true)
            } else {
                display(false)
            }
        }
    })
    // if the person uses the escape key, it will exit the resource
    document.onkeyup = function (data) {
        if (data.which == 27) {
            $.post('http://AS_StealF/exit', JSON.stringify({}));
            return
        }
    };
    $("#close").click(function () {
        $.post('http://AS_StealF/exit', JSON.stringify({}));
        return
    })
    //when the user clicks on the submit button, it will run
    $("#submit").click(function () {
		//var randompass = Math.floor(Math.random() * 100) + 1;
        let inputValue = $("#input").val()
        if (inputValue.length >= 1001) {
            $.post("http://AS_StealF/error", JSON.stringify({
                error: "كلمة السر غير صحيحة"
            }))
            return
        }
                else if (!inputValue) {
            $.post("http://AS_StealF/error", JSON.stringify({
                error: "كلمة السر غير صحيحة"
            }))
            return
        }
		       else if (inputValue == 99) {
            $.post("http://AS_StealF/sucess", JSON.stringify({})
			)
            return
		}
        // if there are no errors from above, we can send the data back to the original callback and hanndle it from there
        $.post('http://AS_StealF/main', JSON.stringify({
            text: "كلمة السر غير صحيحة",
        }));
        return;
    })
})

exactly here : else if (inputValue == 99) {
$.post(“http://AS_StealF/sucess”, JSON.stringify({})
)
return


and here is the lua code I want to get the password from,

RegisterNetEvent('Display:password')
AddEventHandler('Display:password', function()
	passwordfd = math.random(1,100)
	chat("Password : "..passwordfd, {0,255,0})
end)

You can tell me what you need overall.
LUA to JS
or JS to LUA then i can help you etc. :wink:

I want the input value, that is in JS, to be the variable passwordfd in Lua @Fixheko

For that you want to do something like this

$.post('https://AS_StealF/submitPassword', JSON.stringify({pw: $('#your-password-input').val()}));

Then in Lua, you’ll wanna do

RegisterNUICallback('submitPassword', function(data, cb)
   local passwordfd = data.pw

  print("Password:", passwordfd)
end)

Notice #your-password-input in first script, you must change that to your password input’s ID

Sure, when you are using $.post you can always add any paramter you want to get data.
for example: JSON.stringify({hello: parseInt(amount)})
Here is your code.

//JS
$.post("http://AS_StealF/submitPassword", JSON.stringify({
    password: $("#passInput").val()
}))
//LUA
RegisterNUICallback("submitPassword", function(data)
    print("Here is your password: ", data.password)
end)
1 Like

Everything @rcore and @Fixheko said is correct. I will just add my two cents: after you received password on client side - send it to server side for verification:

-- client.lua
RegisterNUICallback('submitPassword', function(data, cb)
   local passwordfd = data.pw
   TriggerServerEvent("password:verify", passwordfd)
   print("Password:", passwordfd)
end)
-- server.lua
local correctPassword = "seven-delta-snake"
RegisterNetEvent("password:verify", function(password)
    local playerId = source
    if password == correctPassword then
        TriggerClientEvent("password:correct", playerId)
    end
end)

Try this one

--Server
local correctPassword = "seven-delta-snake"
RegisterNetEvent("password:verify", function(password)
    if password == correctPassword then
        TriggerClientEvent("password:correct", source, password)
    end
end)

--Client
RegisterNetEvent("password:correct", function(password)
    if password then
        print("Password is correct")
    else 
        print("Password is incorrect")
    end
end)

can i use a random password??

No, it is not possible to use random password - don’t even try to that, otherwise your server will crash.

Ok…

What do you mean I can add any parameter, so can I use the variable passwordfd as the password in the JS code,

	passwordfd = math.random(1,100)

RegisterNetEvent('Display:password')
AddEventHandler('Display:password', function()
	chat("Password : "..passwordfd, {0,255,0})
end)

I mean if you want to post data from JS to LUA
by adding paramter with some data for example:

//JavaScript
var amount = $(this).attr("data-amount");
if (amount > 0 ) {
   $.post("http://AS_StealF/submitPassword", JSON.stringify({test: parseInt(amount)})
}
//Lua
RegisterNUICallback("submitPassword", function(data)
  if tonumber(data.amount) ~= nil and tonumber(data.amount) > 0 then
      print("Amount entered is ", data.amount)
  end
end)

or… just look at badgers jail script, it holds a password checker when the command is entered…

For all the people who helped me in this topic, I appreciate you all your help guys, but what I need is :
I need a random password, that changes when ever a specific event is triggered, and I want the password to be between 1,100 and I want it to be the same in Lua and JavaScript, hope you guys can help.

can you make an example callback if u mind