import server from "../../../services/server.js"; import protectedSessionHolder from "../../../services/protected_session_holder.js"; import toastService from "../../../services/toast.js"; const TPL = `

`; export default class ChangePasswordOptions { constructor() { $("#options-password").html(TPL); this.$passwordHeading = $("#password-heading"); this.$form = $("#change-password-form"); this.$oldPassword = $("#old-password"); this.$newPassword1 = $("#new-password1"); this.$newPassword2 = $("#new-password2"); this.$savePasswordButton = $("#save-password-button"); this.$resetPasswordButton = $("#reset-password-button"); this.$resetPasswordButton.on("click", async () => { if (confirm("By resetting the password you will forever lose access to all your existing protected notes. Do you really want to reset the password?")) { await server.post("password/reset?really=yesIReallyWantToResetPasswordAndLoseAccessToMyProtectedNotes"); const options = await server.get('options'); this.optionsLoaded(options); toastService.showError("Password has been reset. Please set new password"); } }); this.$form.on('submit', () => this.save()); } optionsLoaded(options) { const isPasswordSet = options.isPasswordSet === 'true'; $("#old-password-form-group").toggle(isPasswordSet); this.$passwordHeading.text(isPasswordSet ? 'Change password' : 'Set password'); this.$savePasswordButton.text(isPasswordSet ? 'Change password' : 'Set password'); } save() { const oldPassword = this.$oldPassword.val(); const newPassword1 = this.$newPassword1.val(); const newPassword2 = this.$newPassword2.val(); this.$oldPassword.val(''); this.$newPassword1.val(''); this.$newPassword2.val(''); if (newPassword1 !== newPassword2) { toastService.showError("New passwords are not the same."); return false; } server.post('password/change', { 'current_password': oldPassword, 'new_password': newPassword1 }).then(result => { if (result.success) { toastService.showError("Password has been changed. Trilium will be reloaded after you press OK."); // password changed so current protected session is invalid and needs to be cleared protectedSessionHolder.resetProtectedSession(); } else { toastService.showError(result.message); } }); return false; } }