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