annotate src/login.html.luan @ 8:be36282b556a

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