comparison src/change_email.html.luan @ 57:169ac5fdb320

add change email
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 28 Nov 2022 23:47:19 -0700
parents src/register.html.luan@c57b84f461ae
children 31c895b73bd0
comparison
equal deleted inserted replaced
56:7ce54f6d93f2 57:169ac5fdb320
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local Io = require "luan:Io.luan"
4 local Http = require "luan:http/Http.luan"
5 local Shared = require "site:/lib/Shared.luan"
6 local head = Shared.head or error()
7 local header = Shared.header or error()
8 local footer = Shared.footer or error()
9 local base_url = Shared.base_url or error()
10 local call_mail_api = Shared.call_mail_api or error()
11 local Forum = require "site:/lib/Forum.luan"
12 local forum_title = Forum.title or error()
13 local User = require "site:/lib/User.luan"
14 local Db = require "site:/lib/Db.luan"
15 local run_in_transaction = Db.run_in_transaction or error()
16
17
18 local function page(contents)
19 Io.stdout = Http.response.text_writer()
20 %>
21 <!doctype html>
22 <html>
23 <head>
24 <% head() %>
25 <title><%=forum_title%> - Change Email</title>
26 </head>
27 <body>
28 <% header() %>
29 <div content>
30 <h1>Change Email</h1>
31 <%
32 contents()
33 %>
34 </div>
35 <% footer() %>
36 </body>
37 </html>
38 <%
39 end
40
41 return function()
42 local user = User.current_required()
43 if user==nil then return end
44 local email = Http.request.parameters.email
45 local password = Http.request.parameters.password
46 if email == nil then
47 page(function()
48 %>
49 <form>
50 <p>Your current email is <b><%=user.email%></b>.</p>
51 <p>
52 <label>Change email to</label>
53 <input type="email" name="email" autofocus required>
54 </p>
55 <p><input type="submit"></p>
56 </form>
57 <%
58 end)
59 elseif password == nil then
60 run_in_transaction( function()
61 user = user.reload()
62 user.hidden_password = User.new_password()
63 user.save()
64 end )
65 local result = call_mail_api( "change_email", {
66 base_url = base_url()
67 from = forum_title.." <support@freedit.org>"
68 email = email
69 password = user.hidden_password
70 } )
71 result.okay or error(result.error)
72 page(function()
73 %>
74 <p>We have sent an email to your new email address. Click on the link in that email to complete the change.</p>
75 <%
76 end)
77 elseif password ~= user.hidden_password then
78 page(function()
79 %>
80 <p>This link is no longer valid. Please <a href="/change_email.html">try again</a>.</p>
81 <%
82 end)
83 else
84 run_in_transaction( function()
85 user = user.reload()
86 user.email = email
87 user.hidden_password = nil
88 user.save()
89 end )
90 page(function()
91 %>
92 <p>Your email has been change to <b><%=user.email%></b>.</p>
93 <%
94 end)
95 end
96 end