diff --git a/src/plugins/profile/password-reset.js b/src/plugins/profile/password-reset.js index 681ddbc609..450abb7f54 100644 --- a/src/plugins/profile/password-reset.js +++ b/src/plugins/profile/password-reset.js @@ -11,13 +11,15 @@ class PasswordReset extends CustomElement { static get properties () { return { passwords_mismatched: { type: Boolean }, - alert_message: { type: String } + alert_message: { type: String }, + current_password_error: { type: String } } } initialize () { this.passwords_mismatched = false; this.alert_message = ''; + this.current_password_error = ''; } render () { @@ -38,7 +40,21 @@ class PasswordReset extends CustomElement { if (this.checkPasswordsMatch(ev)) return; + const data = new FormData(ev.target); + const current_password = data.get('current_password'); + const password = data.get('password'); + + if (!current_password) { + this.current_password_error = __('Please enter your current password'); + return; + } + this.current_password_error = ''; + + const jid = _converse.session.get('jid'); const domain = _converse.session.get('domain'); + const connection = api.connection.get(); + const bare_jid = Strophe.getBareJidFromJid(jid); + const iq = stx` @@ -60,9 +76,6 @@ class PasswordReset extends CustomElement { const username = iq_response.querySelector('username').textContent; - const data = new FormData(ev.target); - const password = data.get('password'); - const reset_iq = stx` @@ -74,6 +87,8 @@ class PasswordReset extends CustomElement { const iq_result = await api.sendIQ(reset_iq); if (iq_result === null) { this.alert_message = __('Timeout error while trying to set your password'); + } else if (sizzle(`error not-authorized[xmlns="${Strophe.NS.STANZAS}"]`, iq_result).length) { + this.alert_message = __('The current password you provided is incorrect'); } else if (sizzle(`error not-allowed[xmlns="${Strophe.NS.STANZAS}"]`, iq_result).length) { this.alert_message = __('Your server does not allow password reset'); } else if (sizzle(`error forbidden[xmlns="${Strophe.NS.STANZAS}"]`, iq_result).length) { diff --git a/src/plugins/profile/templates/password-reset.js b/src/plugins/profile/templates/password-reset.js index 3b724b6200..2a4befede6 100644 --- a/src/plugins/profile/templates/password-reset.js +++ b/src/plugins/profile/templates/password-reset.js @@ -4,12 +4,28 @@ import { html } from 'lit'; export default el => { const i18n_submit = __('Submit'); const i18n_passwords_must_match = __('The new passwords must match'); + const i18n_current_password = __('Current password'); const i18n_new_password = __('New password'); const i18n_confirm_password = __('Confirm new password'); return html`
el.onSubmit(ev)}> ${el.alert_message ? html`` : ''} +
+ + +
+
modal.querySelector('.passwordreset-form')); + const current_pw_input = form.querySelector('input[name="current_password"]'); + current_pw_input.value = current_password; const pw_input = form.querySelector('input[name="password"]'); pw_input.value = 'secret-password'; const pw_check_input = form.querySelector('input[name="password_check"]'); @@ -150,4 +152,44 @@ describe('The profile modal', function () { expect(alert.textContent).toBe('You are not allowed to change your password'); }), ); + + it( + 'informs you if the current password is incorrect', + mock.initConverse(converse, [], {}, async function (_converse) { + const modal = await submitPasswordResetForm(_converse); + + const sent_IQs = _converse.api.connection.get().IQ_stanzas; + const query_iq = await u.waitUntil(() => + sent_IQs.filter((iq) => sizzle('query[xmlns="jabber:iq:register"]', iq).length).pop(), + ); + + _converse.api.connection.get()._dataRecv( + mock.createRequest(_converse, + u.toStanza(` + + + romeo@montague.lit + + + `), + ), + ); + + const set_iq = await u.waitUntil(() => + sent_IQs.filter((iq) => sizzle('iq[type="set"] query[xmlns="jabber:iq:register"]', iq).length).pop(), + ); + + _converse.api.connection.get()._dataRecv( + mock.createRequest(_converse, + u.toStanza(` + + + `), + ), + ); + + const alert = await u.waitUntil(() => modal.querySelector('.alert-danger')); + expect(alert.textContent).toBe('The current password you provided is incorrect'); + }), + ); });