annotate src/set_name.html.luan @ 61:389e5d8e5f8a default tip

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