comparison src/change_email.html.luan @ 59:02d8876dc41d

improve change email
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Nov 2022 18:45:22 -0700
parents 31c895b73bd0
children
comparison
equal deleted inserted replaced
58:31c895b73bd0 59:02d8876dc41d
48 48
49 return function() 49 return function()
50 local user = User.current_required() 50 local user = User.current_required()
51 if user==nil then return end 51 if user==nil then return end
52 local email = Http.request.parameters.email 52 local email = Http.request.parameters.email
53 local password1 = Http.request.parameters.password1 53 local password = Http.request.parameters.password
54 local password2 = Http.request.parameters.password2 54 if email == nil and password == nil then
55 if email == nil then
56 page(function() 55 page(function()
57 %> 56 %>
58 <form> 57 <form>
59 <p>Your current email is <b><%=user.email%></b>.</p> 58 <p>Your current email is <b><%=user.email%></b>.</p>
60 <p> 59 <p>
63 </p> 62 </p>
64 <p><input type="submit"></p> 63 <p><input type="submit"></p>
65 </form> 64 </form>
66 <% 65 <%
67 end) 66 end)
68 elseif password1 == nil and password2 == nil then 67 elseif email ~= nil and password == nil then
69 run_in_transaction( function() 68 run_in_transaction( function()
70 user = user.reload() 69 user = user.reload()
71 user.hidden_password_1 = User.new_password() 70 user.hidden_password = User.new_password()
71 user.new_email = nil
72 user.save() 72 user.save()
73 end ) 73 end )
74 local result = call_mail_api( "change_email_1", { 74 local result = call_mail_api( "change_email_1", {
75 base_url = base_url() 75 base_url = base_url()
76 from = forum_title.." <support@freedit.org>" 76 from = forum_title.." <support@freedit.org>"
77 to = user.email 77 to = user.email
78 email = email 78 email = email
79 password = user.hidden_password_1 79 password = user.hidden_password
80 } ) 80 } )
81 result.okay or error(result.error) 81 result.okay or error(result.error)
82 page(function() 82 page(function()
83 %> 83 %>
84 <p>We have sent an email to your current email address. Click on the link in that email to change your email.</p> 84 <p>We have sent an email to your current email address. Click on the link in that email to change your email.</p>
85 <% 85 <%
86 end) 86 end)
87 elseif password1 ~= nil then 87 elseif email ~= nil and password ~= nil then
88 if password1 ~= user.hidden_password_1 then 88 if password ~= user.hidden_password then
89 invalid() 89 invalid()
90 return 90 return
91 end 91 end
92 run_in_transaction( function() 92 run_in_transaction( function()
93 user = user.reload() 93 user = user.reload()
94 user.hidden_password_1 = nil 94 user.hidden_password = User.new_password()
95 user.hidden_password_2 = User.new_password() 95 user.new_email = email
96 user.save() 96 user.save()
97 end ) 97 end )
98 local result = call_mail_api( "change_email_2", { 98 local result = call_mail_api( "change_email_2", {
99 base_url = base_url() 99 base_url = base_url()
100 from = forum_title.." <support@freedit.org>" 100 from = forum_title.." <support@freedit.org>"
101 email = email 101 to = email
102 password = user.hidden_password_2 102 password = user.hidden_password
103 } ) 103 } )
104 result.okay or error(result.error) 104 result.okay or error(result.error)
105 page(function() 105 page(function()
106 %> 106 %>
107 <p>We have sent an email to <b><%=email%></b>. Click on the link in that email to complete the change.</p> 107 <p>We have sent an email to <b><%=email%></b>. Click on the link in that email to complete the change.</p>
108 <% 108 <%
109 end) 109 end)
110 elseif password2 ~= nil then 110 elseif email == nil and password ~= nil then
111 if password2 ~= user.hidden_password_2 then 111 if password ~= user.hidden_password then
112 invalid() 112 invalid()
113 return 113 return
114 end 114 end
115 run_in_transaction( function() 115 run_in_transaction( function()
116 user = user.reload() 116 user = user.reload()
117 user.email = email 117 user.email = user.new_email or error()
118 user.hidden_password_2 = nil 118 user.hidden_password = nil
119 user.new_email = nil
119 user.save() 120 user.save()
120 end ) 121 end )
121 page(function() 122 page(function()
122 %> 123 %>
123 <p>Your email has been change to <b><%=user.email%></b>.</p> 124 <p>Your email has been change to <b><%=user.email%></b>.</p>