3
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local String = require "luan:String.luan"
|
|
4 local trim = String.trim or error()
|
29
|
5 local regex = String.regex or error()
|
3
|
6 local Io = require "luan:Io.luan"
|
|
7 local Http = require "luan:http/Http.luan"
|
|
8 local Shared = require "site:/lib/Shared.luan"
|
|
9 local head = Shared.head or error()
|
|
10 local header = Shared.header or error()
|
|
11 local footer = Shared.footer or error()
|
|
12 local Forum = require "site:/lib/Forum.luan"
|
|
13 local forum_title = Forum.title or error()
|
|
14 local User = require "site:/lib/User.luan"
|
|
15 local Db = require "site:/lib/Db.luan"
|
|
16 local run_in_transaction = Db.run_in_transaction or error()
|
|
17
|
|
18
|
29
|
19 local name_regex = regex "^[a-zA-Z0-9_-]+$"
|
|
20
|
3
|
21 local function page(contents)
|
|
22 Io.stdout = Http.response.text_writer()
|
|
23 %>
|
|
24 <!doctype html>
|
|
25 <html>
|
|
26 <head>
|
|
27 <% head() %>
|
54
|
28 <title><%=forum_title%> - Set Name</title>
|
3
|
29 </head>
|
|
30 <body>
|
|
31 <% header() %>
|
|
32 <div content>
|
54
|
33 <h1>Set Name</h1>
|
3
|
34 <%
|
|
35 contents()
|
|
36 %>
|
|
37 </div>
|
|
38 <% footer() %>
|
|
39 </body>
|
|
40 </html>
|
|
41 <%
|
|
42 end
|
|
43
|
|
44 return function()
|
|
45 local email = Http.request.parameters.email
|
|
46 local password = Http.request.parameters.password
|
|
47 local name = Http.request.parameters.name
|
54
|
48 local user = User.get_by_email(email)
|
|
49 user or error "email not found"
|
|
50 user.password == password or error "wrong password"
|
|
51 local error_message = nil
|
|
52 if Http.request.method == "POST" then
|
3
|
53 name = trim(name)
|
29
|
54 name_regex.matches(name) or error "invalid name"
|
3
|
55 run_in_transaction( function()
|
54
|
56 user = user.reload()
|
3
|
57 if user.name ~= name and User.get_by_name(name) ~= nil then
|
|
58 error_message = "Name already in use"
|
|
59 else
|
|
60 user.name = name
|
|
61 user.save()
|
|
62 end
|
|
63 end )
|
54
|
64 if error_message == nil then
|
|
65 user.login()
|
3
|
66 page(function()
|
|
67 %>
|
54
|
68 <p>You are now logged in.</p>
|
3
|
69 <%
|
|
70 end)
|
54
|
71 return
|
3
|
72 end
|
|
73 end
|
54
|
74 page(function()
|
|
75 if error_message ~= nil then %>
|
|
76 <p error>Error: <%= error_message %></p>
|
|
77 <% end %>
|
|
78 <form action="set_name.html" method=post>
|
|
79 <input type="hidden" name="email" value="<%= user.email %>" >
|
|
80 <input type="hidden" name="password" value="<%= user.password %>" >
|
|
81 <label>User name for <%= user.email %></label>
|
|
82 <input type="text" name="name" value="<%= name or "" %>" autofocus required pattern="[a-zA-Z0-9_-]+">
|
|
83 <input type="submit" value="Set">
|
|
84 </form>
|
|
85 <%
|
|
86 end)
|
3
|
87 end
|