diff --git a/assets/js/components/account/interface.js b/assets/js/components/account/interface.js index da754ae5..93799c4a 100644 --- a/assets/js/components/account/interface.js +++ b/assets/js/components/account/interface.js @@ -74,6 +74,37 @@ ComunicWeb.components.account.interface = { ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback); }, + /** + * Get passowrd reset token using security answer + * + * @param {String} email The email address of the account + * @param {Array} answers The answers to the security questions + * @param {Function} callback + */ + resetPasswordWithSecurityAnswers: function(email, answers, callback){ + + //Prepare answers + answersText = ""; + answers.forEach(function(answer){ + + if(answersText != "") + answersText += "&"; + + answersText += encodeURIComponent(answer); + + }); + + //Perform the request + var apiURI = "account/check_security_answers"; + var params = { + email: email, + answers: answersText + }; + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback); + + }, + + /** * Request the export of all the data of the user * diff --git a/assets/js/pages/passwordForgotten/promptSecurityQuestions.js b/assets/js/pages/passwordForgotten/promptSecurityQuestions.js index 03d6377d..f7d0bf80 100644 --- a/assets/js/pages/passwordForgotten/promptSecurityQuestions.js +++ b/assets/js/pages/passwordForgotten/promptSecurityQuestions.js @@ -49,6 +49,89 @@ ComunicWeb.pages.passwordForgotten.promptSecurityQuestions = { * @param {HTMLElement} target The target for the form */ _display_form: function(email, questions, target){ - console.log(questions); + + //Create form target + var form = createElem2({ + appendTo: target, + type: "div" + }); + + //Add messages target + var messagesTarget = createElem2({ + appendTo: form, + type: "div" + }); + + //Add notice + add_p(form, "Please answer your security questions now, in order to reset your password."); + add_p(form, "Do not worry about lowercase and uppercase letters."); + + //Process the questions + var inputs = []; + + questions.forEach(function(question){ + + //Create the input + var input = createFormGroup({ + target: form, + label: question, + placeholder: "Your answer to the question", + type: "text" + }); + inputs.push(input); + + }); + + //Add submit button + var submit = createElem2({ + appendTo: form, + type: "div", + class: "btn btn-primary", + innerHTML: "Submit" + }); + + + //Make submit button lives + submit.addEventListener("click", function(e){ + + //Check if another request is already pending + if(submit.disabled) return; + + //Check the inputs + var answers = []; + inputs.forEach(function(input){ + answers.push(input.value); + }); + + //Send a request to the server + submit.disabled = true; + + //Add loading message + emptyElem(messagesTarget); + messagesTarget.appendChild(ComunicWeb.common.messages.createCalloutElem( + "Loading", + "Please wait while we are checking your security answers...", + "info")); + + ComunicWeb.components.account.interface.resetPasswordWithSecurityAnswers(email, answers, function(result){ + + submit.disabled = false; + emptyElem(messagesTarget); + + //Check for errors + if(result.error){ + messagesTarget.appendChild(ComunicWeb.common.messages.createCalloutElem( + "Error", + "The server rejected your security answers, please check them...", + "danger" + )); + return; + } + + //Redirect to reset page + openPage("reset_password?token=#" + result.reset_token); + + }); + }); }, } \ No newline at end of file