Mercurial Hosting > freedit
comparison src/set_name.html.luan @ 54:260abd8f8565
login and register
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 27 Nov 2022 23:46:27 -0700 |
parents | src/login.html.luan@a1db5223ced1 |
children | 7ce54f6d93f2 |
comparison
equal
deleted
inserted
replaced
53:cac477dd1f82 | 54:260abd8f8565 |
---|---|
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 regex = String.regex or error() | |
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 | |
19 local name_regex = regex "^[a-zA-Z0-9_-]+$" | |
20 | |
21 local function page(contents) | |
22 Io.stdout = Http.response.text_writer() | |
23 %> | |
24 <!doctype html> | |
25 <html> | |
26 <head> | |
27 <% head() %> | |
28 <title><%=forum_title%> - Set Name</title> | |
29 </head> | |
30 <body> | |
31 <% header() %> | |
32 <div content> | |
33 <h1>Set Name</h1> | |
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 | |
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 | |
53 name = trim(name) | |
54 name_regex.matches(name) or error "invalid name" | |
55 run_in_transaction( function() | |
56 user = user.reload() | |
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 ) | |
64 if error_message == nil then | |
65 user.login() | |
66 page(function() | |
67 %> | |
68 <p>You are now logged in.</p> | |
69 <% | |
70 end) | |
71 return | |
72 end | |
73 end | |
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) | |
87 end |