ComunicWeb/assets/js/pages/settings/sections/password.js

136 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

/**
* Password update section
*
* @author Pierre HUBERT
*/
ComunicWeb.pages.settings.sections.password = {
/**
* Open settings section
*
* @param {object} args Additionnal arguments
* @param {HTMLElement} target The target for the page
*/
open: function(args, target){
//Create a box
var box = createElem2({
appendTo: target,
type: "div",
class: "box box-primary box-password-settings"
});
//Add box header
var boxHead = createElem2({
appendTo: box,
type: "div",
class: "box-header",
});
var boxTitle = createElem2({
appendTo: boxHead,
type: "h3",
class: "box-title",
innerHTML: "Password update"
});
//Create box body
var boxBody = createElem2({
appendTo: box,
type: "div",
class: "box-body"
});
//Create a form contener
var formContener = createElem2({
appendTo: boxBody,
type: "div"
});
//Ask the user to enter its old password
var oldPasswordInput = createFormGroup({
target: formContener,
label: "Old password",
placeholder: "Your old (current) password",
type: "password"
});
2021-02-17 17:22:43 +00:00
// Ask the user to enter its new password
var newPasswordInput = new PasswordInput(
formContener,
tr("New password"),
tr("Your new password")
);
(async () => {
try {
let user = await userInfo(userID());
newPasswordInput.setUser(user)
let mail = await AccountInterface.getMail()
newPasswordInput.setEmail(mail)
} catch(e) {
console.error(e);
notify(tr("Failed to load your information to prepare password update!"), "danger");
}
2021-02-17 17:22:43 +00:00
})();
//Ask the user to confirm its new password
var confirmNewPasswordInput = createFormGroup({
target: formContener,
label: "Confirm your password",
placeholder: "Your new password",
type: "password"
});
//Add submit button
var sendButton = createElem2({
appendTo: formContener,
type: "div",
class: "btn btn-primary submit-form",
innerHTML: "Update password"
});
//Make submit button lives
sendButton.onclick = function(){
//Check the old and the new password
if(!ComunicWeb.common.formChecker.checkInput(oldPasswordInput, true)){
2021-02-17 17:22:43 +00:00
notify(tr("Please specify your old password to update your password!"), "danger");
return;
}
2021-02-17 17:22:43 +00:00
if(!newPasswordInput.isValid()){
2021-02-17 17:24:19 +00:00
notify(tr("Please specify a valid new password!"), "danger");
return;
}
//Check if the old and the new password are the same or not
if(newPasswordInput.value !== confirmNewPasswordInput.value){
2021-02-17 17:22:43 +00:00
notify(tr("The new password and its confirmation are not the same!"), "danger");
return;
}
//Perform a request over the server
sendButton.style.visibility = "hidden";
//Perform a request over the server
ComunicWeb.components.settings.interface.updatePassword(oldPasswordInput.value, newPasswordInput.value, function(result){
sendButton.style.visibility = "visible";
//Check for errors
if(result.error){
2021-02-17 17:22:43 +00:00
notify(tr("An error occurred while trying to update user password!"), "danger");
return;
}
//Success
2021-02-17 17:22:43 +00:00
notify(tr("Your password has been successfully updated !"));
//Refresh current page to remove passwords (security)
ComunicWeb.common.page.refresh_current_page();
});
}
}
};