Simple login page code using Lua scripting
Posted: Wed Feb 14, 2024 5:22 pm
Hello! I would like to share a simple code for a login page using Lua scripting. You are free to style the HTML codes however you want.
The code can be a little challenging to understand at first, but feel free to ask if you don't understand any part.
See also: Simple register page code using Lua scripting
How to use it:
The code can be a little challenging to understand at first, but feel free to ask if you don't understand any part.
See also: Simple register page code using Lua scripting
How to use it:
- Log in to the Wapka Dashboard (https://web.wapka.org/dashboard);
- Select your website from the "Your services" list;
- Now create a new page called "login" (Basic Function > New Page) - you can skip this step if you already have a login page and want to use it.
- Now open the login page we created and go to Advanced function > Script, and finally paste the following Lua code into the textarea:
Code: Select all
if (env.userid ~= 0) then -- Check if the user is already logged in
url.redirect("/") -- If yes, redirect to the main page
end
if (req.method == "POST") then -- Checks if the login form has been submitted
error_message = nil -- Starts error message as null
username = req.post.username -- Gets the username from the POST request
password = req.post.password -- Gets the password from the POST request
continue = req.get.continue -- Useful if the user came from another page
if continue == nil then continue = "/" end -- Checks if a continue URL has been defined, if not, it is defined as the home page
if (username == "" and password == "") then -- Checks if the username and password are empty
error_message = "You must enter your username and password."
elseif (username == "") then -- Checks if username is empty
error_message = "You must enter your username."
elseif (password == "") then -- Checks if password is empty
error_message = "You must enter your password."
else -- Continue with login if username and password have been filled in
local param = { -- Sets the parameters for the API login method
username = username,
password = password
}
local is_ok, login, info, error_login = api.user_login(param)
if (is_ok) then -- Username and password are correct and login was successful
url.redirect(continue) -- Redirects to the destination defined in continue
else -- An error occurred while trying to login
error_message = error_login -- defines the error message as the one received in the login method
end
end
if (error_message) then -- Checks if any errors occurred and sets an error message div to display on the page
html_error = [=[<div class="error-message">%s</div>]=]
error_message = string.format(html_error, error_message)
end
end
local html_code = [=[
<h2>Log-in to your account</h2>
%s <!-- This is where the error message will be -->
<form method="post">
<div class="input">
<label for="username">Username:</label><br>
<input type="text" name="username" value="%s" placeholder="Username" id="username">
</div>
<div class="input">
<label for="password">Password:</label><br>
<input type="password" name="password" value="%s" placeholder="Password" id="password">
</div>
<div class="input-button">
<input type="submit" class="input-button" value="Login">
</div>
</form>
<div class="register-message">
Not registered yet? <a href="/register">Register</a>
</div>
]=]
print(string.format(html_code, error_message or "", username or "", password or ""))