annotate src/login.html.luan @ 47:7ef9222474e2

add more button
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 14 Nov 2022 23:01:29 -0700
parents a1db5223ced1
children 260abd8f8565
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()
29
a1db5223ced1 luan changes
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
5 local regex = String.regex or error()
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Html = require "luan:Html.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local url_encode = Html.url_encode or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Io = require "luan:Io.luan"
4
a17e400ddaa1 add email
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
9 local output_of = Io.output_of or error()
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Shared = require "site:/lib/Shared.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local head = Shared.head or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local header = Shared.header or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local footer = Shared.footer or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local base_url = Shared.base_url or error()
6
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
16 local call_mail_api = Shared.call_mail_api or error()
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local Forum = require "site:/lib/Forum.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local forum_title = Forum.title or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local User = require "site:/lib/User.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 local Db = require "site:/lib/Db.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 local run_in_transaction = Db.run_in_transaction or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
29
a1db5223ced1 luan changes
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
24 local name_regex = regex "^[a-zA-Z0-9_-]+$"
a1db5223ced1 luan changes
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
25
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 local function get_user(email,password)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 local user = User.get_by_email(email)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 user or error "email not found"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 user.password == password or error "wrong password"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 local function login(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 Http.response.set_persistent_cookie("user",user.name)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 Http.response.set_persistent_cookie("password",user.password)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 Http.request.cookies.user = user.name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 Http.request.cookies.password = user.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 local function register_form(user,name,error_message)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 if error_message ~= nil then %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 <p error>Error: <%= error_message %></p>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 <% end %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 <form>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 <input type="hidden" name="email" value="<%= user.email %>" >
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 <input type="hidden" name="password" value="<%= user.password %>" >
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 <label>User name for <%= user.email %></label>
14
7d0c96408abf restrict name
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
48 <input type="text" name="name" value="<%= name or "" %>" autofocus required pattern="[a-zA-Z0-9_-]+">
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 <input type="submit" value="Register">
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 </form>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 local function page(contents)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 Io.stdout = Http.response.text_writer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 <!doctype html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 <html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 <head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 <% head() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 <title><%=forum_title%> - Login or Register</title>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 </head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 <body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 <% header() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 <div content>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 <h1>Login or Register</h1>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 contents()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 <% footer() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 </body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 </html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 return function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 local email = Http.request.parameters.email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 local password = Http.request.parameters.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 local name = Http.request.parameters.name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 if email == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 <form>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 <label>Email address</label>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 <input type="email" name="email" autofocus required>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 <input type="submit" value="Login or Register">
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 </form>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 elseif password == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 local user = User.get_or_create_by_email(email)
6
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
93 local result = call_mail_api( "login_email", {
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
94 base_url = base_url()
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
95 from = forum_title.." <support@freedit.org>"
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
96 email = user.email
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
97 password = user.password
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
98 } )
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
99 result.okay or error(result.error)
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 <p>We have sent you an email. Please check your email to login or register.</p>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 elseif name == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 local user = get_user(email,password)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 if user.name == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 register_form(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 login(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 <p>You are now logged in.</p>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 name = trim(name)
29
a1db5223ced1 luan changes
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
121 name_regex.matches(name) or error "invalid name"
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 local error_message = nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 local user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 run_in_transaction( function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 user = get_user(email,password)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 if user.name ~= name and User.get_by_name(name) ~= nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 error_message = "Name already in use"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 user.name = name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 user.save()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 end )
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 if error_message ~= nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 register_form(user,name,error_message)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 login(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 <p>You are now registered.</p>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 end