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