annotate src/login.html.luan @ 4:a17e400ddaa1

add email
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 21 Jun 2022 11:58:27 -0600
parents fc3ee39d7764
children 9166f6a14021
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()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local Forum = require "site:/lib/Forum.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local forum_title = Forum.title or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local User = require "site:/lib/User.luan"
4
a17e400ddaa1 add email
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
18 local Mail = require "site:/lib/Mail.luan"
3
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)
4
a17e400ddaa1 add email
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
90 local mailer = Mail.sender() or error "mail not configured"
a17e400ddaa1 add email
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
91 mailer.send{
a17e400ddaa1 add email
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
92 From = forum_title.." <support@freedit.org>"
a17e400ddaa1 add email
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
93 To = user.email
a17e400ddaa1 add email
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
94 Subject = "Login"
a17e400ddaa1 add email
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
95 body = output_of(function() %>
a17e400ddaa1 add email
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
96 Login or register by clicking this link:
a17e400ddaa1 add email
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
97
a17e400ddaa1 add email
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
98 <%=base_url()%>/login.html?email=<%=url_encode(email)%>&password=<%=user.password%>
a17e400ddaa1 add email
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
99 <% end)
a17e400ddaa1 add email
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
100 }
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 <p>We have sent you an email. Please check your email to login or register.</p>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 elseif name == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 local user = get_user(email,password)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 if user.name == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 register_form(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 login(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 <p>You are now logged in.</p>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 name = trim(name)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 #name > 0 or error "empty name"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 local error_message = nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 local user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 run_in_transaction( function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 user = get_user(email,password)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 if user.name ~= name and User.get_by_name(name) ~= nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 error_message = "Name already in use"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 user.name = name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 user.save()
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 if error_message ~= nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 register_form(user,name,error_message)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 end)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 login(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 <p>You are now registered.</p>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 <%
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
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 end