comparison mod_register_web/mod_register_web.lua @ 2929:3a104a900af1

mod_register_web: Pass username to generate_register_response
author Michel Le Bihan <michel@lebihan.pl>
date Tue, 13 Mar 2018 21:12:21 +0100
parents 049975800d1c
children d631fd9a3300
comparison
equal deleted inserted replaced
2928:b0d92332b87f 2929:3a104a900af1
115 115
116 function register_user(form, origin) 116 function register_user(form, origin)
117 local username = form.username; 117 local username = form.username;
118 local password = form.password; 118 local password = form.password;
119 local confirm_password = form.confirm_password; 119 local confirm_password = form.confirm_password;
120 local jid = nil;
120 form.username, form.password, form.confirm_password = nil, nil, nil; 121 form.username, form.password, form.confirm_password = nil, nil, nil;
121 122
122 local prepped_username = nodeprep(username); 123 local prepped_username = nodeprep(username);
123 if not prepped_username then 124 if not prepped_username then
124 return nil, "Username contains forbidden characters"; 125 return nil, "Username contains forbidden characters";
137 if confirm_password ~= password then 138 if confirm_password ~= password then
138 return nil, "Passwords don't match"; 139 return nil, "Passwords don't match";
139 end 140 end
140 local ok, err = usermanager.create_user(prepped_username, password, module.host); 141 local ok, err = usermanager.create_user(prepped_username, password, module.host);
141 if ok then 142 if ok then
143 jid = prepped_username.."@"..module.host
142 local extra_data = {}; 144 local extra_data = {};
143 for field in pairs(extra_fields) do 145 for field in pairs(extra_fields) do
144 local field_value = form[field]; 146 local field_value = form[field];
145 if field_value and #field_value > 0 then 147 if field_value and #field_value > 0 then
146 extra_data[field] = field_value; 148 extra_data[field] = field_value;
154 host = module.host, 156 host = module.host,
155 source = module.name, 157 source = module.name,
156 ip = origin.conn:ip(), 158 ip = origin.conn:ip(),
157 }); 159 });
158 end 160 end
159 return ok, err; 161 return jid, err;
160 end 162 end
161 163
162 function generate_success(event, form) 164 function generate_success(event, jid)
163 return render(success_tpl, { jid = nodeprep(form.username).."@"..module.host }); 165 return render(success_tpl, { jid = jid });
164 end 166 end
165 167
166 function generate_register_response(event, form, ok, err) 168 function generate_register_response(event, jid, err)
167 event.response.headers.content_type = "text/html; charset=utf-8"; 169 event.response.headers.content_type = "text/html; charset=utf-8";
168 if ok then 170 if jid then
169 return generate_success(event, form); 171 return generate_success(event, jid);
170 else 172 else
171 return generate_page(event, { register_error = err }); 173 return generate_page(event, { register_error = err });
172 end 174 end
173 end 175 end
174 176
175 function handle_form(event) 177 function handle_form(event)
176 local request, response = event.request, event.response; 178 local request, response = event.request, event.response;
177 local form = http.formdecode(request.body); 179 local form = http.formdecode(request.body);
178 verify_captcha(request, form, function (ok, err) 180 verify_captcha(request, form, function (ok, err)
179 if ok then 181 if ok then
180 local register_ok, register_err = register_user(form, request); 182 local jid, register_err = register_user(form, request);
181 response:send(generate_register_response(event, form, register_ok, register_err)); 183 response:send(generate_register_response(event, jid, register_err));
182 else 184 else
183 response:send(generate_page(event, { register_error = err })); 185 response:send(generate_page(event, { register_error = err }));
184 end 186 end
185 end); 187 end);
186 return true; -- Leave connection open until we respond above 188 return true; -- Leave connection open until we respond above