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

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 06 Dec 2022 13:37:25 -0700
parents c57b84f461ae
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 Html = require "luan:Html.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local url_encode = Html.url_encode 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"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local function page(contents)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 Io.stdout = Http.response.text_writer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 <!doctype html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 <html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 <head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 <% head() %>
54
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
23 <title><%=forum_title%> - Login</title>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
24 <script>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
25 function show(checkbox) {
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
26 document.querySelector('input[name="password"]').type = checkbox.checked ? 'text' : 'password';
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
27 }
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
28 </script>
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 </head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 <body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 <% header() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 <div content>
54
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
33 <h1>Login</h1>
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 contents()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 <% footer() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 </body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 </html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 return function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 local email = Http.request.parameters.email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 local password = Http.request.parameters.password
54
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
47 local error_message = nil
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
48 if Http.request.method == "POST" then
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
49 local user = User.get_by_email(email)
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
50 if user == nil then
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
51 error_message = "email not found"
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
52 elseif user.password ~= password then
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
53 error_message = "wrong password"
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
54 end
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
55 if error_message == nil then
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
56 if user.name == nil then
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
57 Http.response.send_redirect("/set_name.html?email="..url_encode(email).."&password="..password)
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
58 return
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
59 end
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
60 user.login()
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
61 page(function()
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 %>
54
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
63 <p>You are now logged in.</p>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
64 <%
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
65 end)
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
66 return
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
67 end
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
68 end
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
69 page(function()
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
70 if error_message ~= nil then %>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
71 <p error>Error: <%= error_message %></p>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
72 <% end %>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
73 <form action="login.html" method=post>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
74 <p>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
75 <label>Email address</label>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
76 <input type="email" name="email" value="<%= email or "" %>" autofocus required>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
77 </p>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
78 <p>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
79 <label>Password</label>
55
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
80 <input type="password" name="password" value="<%= password or "" %>" required>
54
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
81 <label clickable><input type=checkbox onclick="show(this)">Show</label>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
82 </p>
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
83 <p><input type="submit" value="Login"></p>
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 </form>
55
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
85 <p><a href="/register.html">Get or change password</a></p>
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 <%
54
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 29
diff changeset
87 end)
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 end