comparison src/login.html.luan @ 3:fc3ee39d7764

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