annotate src/change_email.html.luan @ 61:389e5d8e5f8a default tip

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 06 Dec 2022 13:37:25 -0700
parents 02d8876dc41d
children
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 Io = require "luan:Io.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local Shared = require "site:/lib/Shared.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local head = Shared.head or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local header = Shared.header or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local footer = Shared.footer or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local base_url = Shared.base_url or error()
6
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
10 local call_mail_api = Shared.call_mail_api or error()
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Forum = require "site:/lib/Forum.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local forum_title = Forum.title or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local User = require "site:/lib/User.luan"
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
14 local Db = require "site:/lib/Db.luan"
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
15 local run_in_transaction = Db.run_in_transaction or error()
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local function page(contents)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 Io.stdout = Http.response.text_writer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 <!doctype html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 <html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 <head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 <% head() %>
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
25 <title><%=forum_title%> - Change Email</title>
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 </head>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 <body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 <% header() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 <div content>
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
30 <h1>Change Email</h1>
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 contents()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 <% footer() %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 </body>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 </html>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40
58
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
41 local function invalid()
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
42 page(function()
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
43 %>
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
44 <p>This link is no longer valid. Please <a href="/change_email.html">try again</a>.</p>
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
45 <%
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
46 end)
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
47 end
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
48
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 return function()
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
50 local user = User.current_required()
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
51 if user==nil then return end
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 local email = Http.request.parameters.email
59
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
53 local password = Http.request.parameters.password
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
54 if email == nil and password == nil then
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 %>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 <form>
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
58 <p>Your current email is <b><%=user.email%></b>.</p>
55
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
59 <p>
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
60 <label>Change email to</label>
55
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
61 <input type="email" name="email" autofocus required>
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
62 </p>
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
63 <p><input type="submit"></p>
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 </form>
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 end)
59
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
67 elseif email ~= nil and password == nil then
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
68 run_in_transaction( function()
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
69 user = user.reload()
59
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
70 user.hidden_password = User.new_password()
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
71 user.new_email = nil
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
72 user.save()
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
73 end )
58
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
74 local result = call_mail_api( "change_email_1", {
6
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
75 base_url = base_url()
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
76 from = forum_title.." <support@freedit.org>"
58
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
77 to = user.email
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
78 email = email
59
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
79 password = user.hidden_password
6
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
80 } )
9166f6a14021 add email api
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
81 result.okay or error(result.error)
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 page(function()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 %>
58
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
84 <p>We have sent an email to your current email address. Click on the link in that email to change your email.</p>
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
85 <%
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
86 end)
59
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
87 elseif email ~= nil and password ~= nil then
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
88 if password ~= user.hidden_password then
58
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
89 invalid()
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
90 return
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
91 end
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
92 run_in_transaction( function()
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
93 user = user.reload()
59
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
94 user.hidden_password = User.new_password()
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
95 user.new_email = email
58
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
96 user.save()
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
97 end )
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
98 local result = call_mail_api( "change_email_2", {
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
99 base_url = base_url()
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
100 from = forum_title.." <support@freedit.org>"
59
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
101 to = email
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
102 password = user.hidden_password
58
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
103 } )
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
104 result.okay or error(result.error)
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
105 page(function()
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
106 %>
58
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
107 <p>We have sent an email to <b><%=email%></b>. Click on the link in that email to complete the change.</p>
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
108 <%
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
109 end)
59
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
110 elseif email == nil and password ~= nil then
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
111 if password ~= user.hidden_password then
58
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
112 invalid()
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
113 return
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
114 end
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
115 run_in_transaction( function()
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
116 user = user.reload()
59
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
117 user.email = user.new_email or error()
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
118 user.hidden_password = nil
02d8876dc41d improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
119 user.new_email = nil
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
120 user.save()
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
121 end )
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
122 page(function()
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
123 %>
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
124 <p>Your email has been change to <b><%=user.email%></b>.</p>
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 <%
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 end)
58
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
127 else
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
128 error()
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 end