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