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