Mercurial Hosting > linkmystyle
diff src/register.js.luan @ 0:8f4df159f06b
start public repo
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Fri, 11 Jul 2025 20:57:49 -0600 |
| parents | |
| children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/register.js.luan Fri Jul 11 20:57:49 2025 -0600 @@ -0,0 +1,97 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local String = require "luan:String.luan" +local to_lower = String.lower or error() +local Html = require "luan:Html.luan" +local html_encode = Html.encode or error() +local Io = require "luan:Io.luan" +local Http = require "luan:http/Http.luan" +local User = require "site:/lib/User.luan" +local name_regex = User.name_regex +local new_code = User.new_code +local Utils = require "site:/lib/Utils.luan" +local email_regex = Utils.email_regex +local base_url = Utils.base_url or error() +local warn = Utils.warn or error() +local Shared = require "site:/lib/Shared.luan" +local js_error = Shared.js_error or error() +local send_mail_async = Shared.send_mail_async or error() +local Db = require "site:/lib/Db.luan" +local run_in_transaction = Db.run_in_transaction or error() +local Logging = require "luan:logging/Logging.luan" +local logger = Logging.logger "register.js" + + +return function() + Io.stdout = Http.response.text_writer() + local username = Http.request.parameters.username or error() + local email = Http.request.parameters.email or error() + local password = Http.request.parameters.password or error() + email_regex.matches(email) or error("bad email: "..email) + if not name_regex.matches(username) then + js_error( "username", [[Usernames may only contain letters, numbers, underscores ("_") and hyphens ("-")]] ) + return + end + local user + local err_fld, err_msg = run_in_transaction( function() + user = User.get_by_email(email) + if user == nil then + if User.get_by_name(username) ~= nil then + return "username", "This username is already taken" + end + user = User.new{ name=username, email=email, password=password } + else + if to_lower(user.name) ~= to_lower(username) and User.get_by_name(username) ~= nil then + return "username", "This username is already taken" + end + if user.registered ~= nil then + return "email", "This email is already in use" + end + user.name = username + user.password = password + end + user.code = user.code or new_code() + user.save() + end ) + if err_fld ~= nil then + js_error(err_fld,err_msg) + return + end + logger.info("code = "..user.code) + local url = base_url().."/register2.html?user="..user.name.."&code="..user.code + send_mail_async { + From = "Link My Style <support@linkmy.style>" + To = email + Subject = "Confirmation Code" + ["MIME-Version"] = "1.0" + ["Content-Type"] = "multipart/alternative" + body = { + { + ["Content-Type"] = [[text/plain; charset="UTF-8"]] + body = `%> +Thank you for registering. Please click the link below or use the 6 digit confirmation code to complete the process: + +<%=url%> + +Confirmation Code: <%=user.code%> + +If you did not request this code, please ignore this email. +<% ` + } + { + ["Content-Type"] = [[text/html; charset="UTF-8"]] + body = `%> +Thank you for registering. Please <a href="<%=html_encode(url)%>">click here</a> or use the 6 digit confirmation code below to complete the process:<br> +<br> +Confirmation Code: <b><%=user.code%></b><br> +<br> +If you did not request this code, please ignore this email.<br> +<% ` + } + } + } +%> + clearErrors(context.form); + location = '/register2.html?user=<%=username%>'; +<% +end
