comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:8f4df159f06b
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local String = require "luan:String.luan"
4 local to_lower = String.lower or error()
5 local Html = require "luan:Html.luan"
6 local html_encode = Html.encode or error()
7 local Io = require "luan:Io.luan"
8 local Http = require "luan:http/Http.luan"
9 local User = require "site:/lib/User.luan"
10 local name_regex = User.name_regex
11 local new_code = User.new_code
12 local Utils = require "site:/lib/Utils.luan"
13 local email_regex = Utils.email_regex
14 local base_url = Utils.base_url or error()
15 local warn = Utils.warn or error()
16 local Shared = require "site:/lib/Shared.luan"
17 local js_error = Shared.js_error or error()
18 local send_mail_async = Shared.send_mail_async or error()
19 local Db = require "site:/lib/Db.luan"
20 local run_in_transaction = Db.run_in_transaction or error()
21 local Logging = require "luan:logging/Logging.luan"
22 local logger = Logging.logger "register.js"
23
24
25 return function()
26 Io.stdout = Http.response.text_writer()
27 local username = Http.request.parameters.username or error()
28 local email = Http.request.parameters.email or error()
29 local password = Http.request.parameters.password or error()
30 email_regex.matches(email) or error("bad email: "..email)
31 if not name_regex.matches(username) then
32 js_error( "username", [[Usernames may only contain letters, numbers, underscores ("_") and hyphens ("-")]] )
33 return
34 end
35 local user
36 local err_fld, err_msg = run_in_transaction( function()
37 user = User.get_by_email(email)
38 if user == nil then
39 if User.get_by_name(username) ~= nil then
40 return "username", "This username is already taken"
41 end
42 user = User.new{ name=username, email=email, password=password }
43 else
44 if to_lower(user.name) ~= to_lower(username) and User.get_by_name(username) ~= nil then
45 return "username", "This username is already taken"
46 end
47 if user.registered ~= nil then
48 return "email", "This email is already in use"
49 end
50 user.name = username
51 user.password = password
52 end
53 user.code = user.code or new_code()
54 user.save()
55 end )
56 if err_fld ~= nil then
57 js_error(err_fld,err_msg)
58 return
59 end
60 logger.info("code = "..user.code)
61 local url = base_url().."/register2.html?user="..user.name.."&code="..user.code
62 send_mail_async {
63 From = "Link My Style <support@linkmy.style>"
64 To = email
65 Subject = "Confirmation Code"
66 ["MIME-Version"] = "1.0"
67 ["Content-Type"] = "multipart/alternative"
68 body = {
69 {
70 ["Content-Type"] = [[text/plain; charset="UTF-8"]]
71 body = `%>
72 Thank you for registering. Please click the link below or use the 6 digit confirmation code to complete the process:
73
74 <%=url%>
75
76 Confirmation Code: <%=user.code%>
77
78 If you did not request this code, please ignore this email.
79 <% `
80 }
81 {
82 ["Content-Type"] = [[text/html; charset="UTF-8"]]
83 body = `%>
84 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>
85 <br>
86 Confirmation Code: <b><%=user.code%></b><br>
87 <br>
88 If you did not request this code, please ignore this email.<br>
89 <% `
90 }
91 }
92 }
93 %>
94 clearErrors(context.form);
95 location = '/register2.html?user=<%=username%>';
96 <%
97 end