[Updated] Simple register page code using Lua scripting
Posted: Sat Feb 17, 2024 9:58 pm
Hello! Here is a simple code for a register 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.
Also see: Simple login 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.
Also see: Simple login 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 "register" (Basic Function > New Page) - you can skip this step if you already have a register page and want to use it.
- Now open the register page we created and go to Advanced function > Script, and finally paste the following Lua code into the textarea:
Code: Select all
-- Checks if a continue URL has been defined. If not, it is defined as the Home page
continue = req.get.continue or "/"
-- Check if the user is already logged in. If yes, redirect to continue URL
if (env.userid ~= 0) then
url.redirect(continue)
end
-- Checks if the register form has been submitted
if (req.method == "POST" and req.post.process_register) then
error_message = nil -- Starts error message as null
email = req.post.email -- Gets the email from the POST request
username = req.post.username -- Gets the username from the POST request
password = req.post.password -- Gets the password from the POST request
fullname = req.post.fullname -- Gets the full name as extra data to define a profile variable
-- Checking whether the fields have been filled in
if (email == "" and
username == "" and
password == "" and
fullname == "") then
error_message = "You must enter all the fields."
elseif (email == "") then
error_message = "You must enter your email."
elseif (username == "") then
error_message = "You must enter your username."
elseif (username == "") then
error_message = "You must enter your password."
elseif (fullname == "") then
error_message = "You must enter your full name."
-- Continuing if all fields have been filled in
else
-- Sets the parameters for the API register method
local param = {
email = email,
username = username,
password = password,
var = {
fullname = fullname -- Sets the full name as a custom variable!
}
}
local is_ok, register, info, error_register = api.user_create(param)
-- Checks whether the registration was successful
if (is_ok) then
url.redirect(continue)
else -- An error occurred while trying to register
error_message = error_register -- Defines the error message as the one received in the register method
end
end
-- Checks if any errors occurred and sets an error message div to display on the page
if (error_message) then
html_error = [=[<div class="error-message">%s</div>]=]
error_message = string.format(html_error, error_message)
end
end
local html_code = [=[
<h2>Register an account</h2>
%s <!-- This is where the error message will be -->
<form method="post">
<div class="input">
<label for="email">Email:</label><br>
<input type="email" name="email" value="%s" placeholder="Email" id="email">
</div>
<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">
<label for="fullname">Fullname:</label><br>
<input type="text" name="fullname" value="%s" placeholder="Full name" id="fullname">
</div>
<div class="input-button">
<input type="submit" name="process_register" value="Register" class="input-button">
</div>
</form>
<div class="register-message">
Already have an account? <a href="/login">Login</a>
</div>
]=]
print(string.format(html_code, error_message or "", email or "", username or "", password or "", fullname or ""))