annotate src/login.html.luan @ 3:fc3ee39d7764

login
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 19 Jun 2022 20:47:31 -0600
parents
children a17e400ddaa1
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 Html = require "luan:Html.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local url_encode = Html.url_encode or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Io = require "luan:Io.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Shared = require "site:/lib/Shared.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local head = Shared.head or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local header = Shared.header or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local footer = Shared.footer or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local base_url = Shared.base_url or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local Forum = require "site:/lib/Forum.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local forum_title = Forum.title or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local User = require "site:/lib/User.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local Db = require "site:/lib/Db.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local run_in_transaction = Db.run_in_transaction or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 local function get_user(email,password)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 local user = User.get_by_email(email)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 user or error "email not found"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 user.password == password or error "wrong password"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 local function login(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 Http.response.set_persistent_cookie("user",user.name)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 Http.response.set_persistent_cookie("password",user.password)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 Http.request.cookies.user = user.name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 Http.request.cookies.password = user.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 local function register_form(user,name,error_message)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 if error_message ~= nil then %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 <p error>Error: <%= error_message %></p>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 <% end %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 <form>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 <input type="hidden" name="email" value="<%= user.email %>" >
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 <input type="hidden" name="password" value="<%= user.password %>" >
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 <label>User name for <%= user.email %></label>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 <input type="text" name="name" value="<%= name or "" %>" autofocus required>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 <input type="submit" value="Register">
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 </form>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 local function page(contents)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 Io.stdout = Http.response.text_writer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 <!doctype html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 <html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 <head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 <% head() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 <title><%=forum_title%> - Login or Register</title>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 </head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 <body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 <% header() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 <div content>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 <h1>Login or Register</h1>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 contents()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 <% footer() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 </body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 </html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 return function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 local email = Http.request.parameters.email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 local password = Http.request.parameters.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 local name = Http.request.parameters.name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 if email == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 <form>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 <label>Email address</label>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 <input type="email" name="email" autofocus required>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 <input type="submit" value="Login or Register">
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 </form>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 elseif password == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 local user = User.get_or_create_by_email(email)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 <p>We have sent you an email. Please check your email to login or register.</p>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 <p>hack - <a href="<%=base_url()%>/login.html?email=<%=url_encode(email)%>&password=<%=user.password%>">link</a></p>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 elseif name == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 local user = get_user(email,password)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 if user.name == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 register_form(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 login(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 <p>You are now logged in.</p>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 name = trim(name)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 #name > 0 or error "empty name"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 local error_message = nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 local user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 run_in_transaction( function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 user = get_user(email,password)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 if user.name ~= name and User.get_by_name(name) ~= nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 error_message = "Name already in use"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 user.name = name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 user.save()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 end )
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 if error_message ~= nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 register_form(user,name,error_message)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 login(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 <p>You are now registered.</p>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 end