2022-06-16 22:53:11 +02:00
|
|
|
import server from "../../../services/server.js";
|
|
|
|
import protectedSessionHolder from "../../../services/protected_session_holder.js";
|
|
|
|
import toastService from "../../../services/toast.js";
|
2019-08-21 20:24:37 +02:00
|
|
|
|
2019-11-03 19:06:22 +01:00
|
|
|
const TPL = `
|
2021-12-30 22:54:08 +01:00
|
|
|
<h3 id="password-heading"></h3>
|
2020-10-29 20:57:26 +01:00
|
|
|
|
|
|
|
<div class="alert alert-warning" role="alert" style="font-weight: bold; color: red !important;">
|
2022-11-08 22:42:36 +01:00
|
|
|
Please take care to remember your new password. Password is used for logging into the web interface and
|
|
|
|
to encrypt protected notes. If you forget your password, then all your protected notes are forever lost.
|
2021-12-30 22:54:08 +01:00
|
|
|
In case you did forget your password, <a id="reset-password-button" href="javascript:">click here to reset it</a>.
|
2020-10-29 20:57:26 +01:00
|
|
|
</div>
|
|
|
|
|
2019-11-03 19:06:22 +01:00
|
|
|
<form id="change-password-form">
|
2021-12-30 22:54:08 +01:00
|
|
|
<div class="form-group" id="old-password-form-group">
|
2019-11-03 19:06:22 +01:00
|
|
|
<label for="old-password">Old password</label>
|
|
|
|
<input class="form-control" id="old-password" type="password">
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="form-group">
|
|
|
|
<label for="new-password1">New password</label>
|
|
|
|
<input class="form-control" id="new-password1" type="password">
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="form-group">
|
2020-07-24 16:03:39 -04:00
|
|
|
<label for="new-password2">New password Confirmation</label>
|
2019-11-03 19:06:22 +01:00
|
|
|
<input class="form-control" id="new-password2" type="password">
|
|
|
|
</div>
|
|
|
|
|
2021-12-30 22:54:08 +01:00
|
|
|
<button class="btn btn-primary" id="save-password-button">Change password</button>
|
2019-11-03 19:06:22 +01:00
|
|
|
</form>`;
|
|
|
|
|
2019-08-21 20:24:37 +02:00
|
|
|
export default class ChangePasswordOptions {
|
|
|
|
constructor() {
|
2021-12-30 23:55:36 +01:00
|
|
|
$("#options-password").html(TPL);
|
2019-11-03 19:06:22 +01:00
|
|
|
|
2021-12-30 22:54:08 +01:00
|
|
|
this.$passwordHeading = $("#password-heading");
|
2019-08-21 20:24:37 +02:00
|
|
|
this.$form = $("#change-password-form");
|
|
|
|
this.$oldPassword = $("#old-password");
|
|
|
|
this.$newPassword1 = $("#new-password1");
|
|
|
|
this.$newPassword2 = $("#new-password2");
|
2021-12-30 22:54:08 +01:00
|
|
|
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);
|
|
|
|
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError("Password has been reset. Please set new password");
|
2021-12-30 22:54:08 +01:00
|
|
|
}
|
|
|
|
});
|
2019-08-21 20:24:37 +02:00
|
|
|
|
2019-11-09 17:45:22 +01:00
|
|
|
this.$form.on('submit', () => this.save());
|
2019-08-21 20:24:37 +02:00
|
|
|
}
|
|
|
|
|
2020-03-31 21:47:15 +02:00
|
|
|
optionsLoaded(options) {
|
2021-12-30 22:54:08 +01:00
|
|
|
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');
|
2020-03-28 19:55:02 +01:00
|
|
|
}
|
2019-08-21 20:24:37 +02:00
|
|
|
|
|
|
|
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) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError("New passwords are not the same.");
|
2019-08-21 20:24:37 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.post('password/change', {
|
|
|
|
'current_password': oldPassword,
|
|
|
|
'new_password': newPassword1
|
|
|
|
}).then(result => {
|
|
|
|
if (result.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError("Password has been changed. Trilium will be reloaded after you press OK.");
|
2019-08-21 20:24:37 +02:00
|
|
|
|
|
|
|
// password changed so current protected session is invalid and needs to be cleared
|
|
|
|
protectedSessionHolder.resetProtectedSession();
|
|
|
|
}
|
|
|
|
else {
|
2019-10-20 10:00:18 +02:00
|
|
|
toastService.showError(result.message);
|
2019-08-21 20:24:37 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-24 16:03:39 -04:00
|
|
|
}
|