0
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local String = require "luan:String.luan"
|
|
4 local to_lower = String.lower or error()
|
|
5 local Io = require "luan:Io.luan"
|
|
6 local Http = require "luan:http/Http.luan"
|
|
7 local Shared = require "site:/lib/Shared.luan"
|
|
8 local js_error = Shared.js_error or error()
|
|
9 local send_mail_async = Shared.send_mail_async or error()
|
|
10 local Utils = require "site:/lib/Utils.luan"
|
|
11 local email_regex = Utils.email_regex
|
|
12 local User = require "site:/lib/User.luan"
|
|
13 local new_code = User.new_code
|
|
14 local Db = require "site:/lib/Db.luan"
|
|
15 local run_in_transaction = Db.run_in_transaction or error()
|
|
16
|
|
17
|
|
18 return function()
|
|
19 local user = User.current() or error()
|
|
20 local email = Http.request.parameters.email or error()
|
|
21 email_regex.matches(email) or error("bad email: "..email)
|
|
22 local err_fld, err_msg = run_in_transaction( function()
|
|
23 user = user.reload()
|
|
24 if to_lower(user.email) == to_lower(email) then
|
|
25 return "email", "Email unchanged"
|
|
26 end
|
|
27 if User.get_by_name(email) ~= nil then
|
|
28 return "email", "This email is already taken"
|
|
29 end
|
|
30 user.new_email = email
|
|
31 user.code = new_code()
|
|
32 user.save()
|
|
33 end )
|
|
34 Io.stdout = Http.response.text_writer()
|
|
35 if err_fld ~= nil then
|
|
36 js_error(err_fld,err_msg)
|
|
37 return
|
|
38 end
|
|
39 send_mail_async {
|
|
40 From = "Link My Style <support@linkmy.style>"
|
|
41 To = email
|
|
42 Subject = "Confirmation code"
|
|
43 body = `%>
|
|
44 Here is your 6 digit confirmation code:
|
|
45 <%=user.code%>
|
|
46 <%`
|
|
47 }
|
|
48 %>
|
|
49 clearErrors(context.form);
|
|
50 location = '/change_email.html';
|
|
51 <%
|
|
52 end
|