From 3313572606005cc576a9845c9738a3a7fd571693 Mon Sep 17 00:00:00 2001 From: Philip Allgaier Date: Wed, 2 Dec 2020 16:00:49 +0100 Subject: [PATCH] Improve password change card (#7756) Co-authored-by: Bram Kragten --- src/panels/profile/ha-change-password-card.js | 154 -------------- src/panels/profile/ha-change-password-card.ts | 195 ++++++++++++++++++ src/translations/en.json | 5 +- 3 files changed, 199 insertions(+), 155 deletions(-) delete mode 100644 src/panels/profile/ha-change-password-card.js create mode 100644 src/panels/profile/ha-change-password-card.ts diff --git a/src/panels/profile/ha-change-password-card.js b/src/panels/profile/ha-change-password-card.js deleted file mode 100644 index d2a51d4722..0000000000 --- a/src/panels/profile/ha-change-password-card.js +++ /dev/null @@ -1,154 +0,0 @@ -import "@material/mwc-button"; -import "@polymer/paper-dialog/paper-dialog"; -import "../../components/ha-circular-progress"; -import { html } from "@polymer/polymer/lib/utils/html-tag"; -/* eslint-plugin-disable lit */ -import { PolymerElement } from "@polymer/polymer/polymer-element"; -import "../../components/ha-card"; -import LocalizeMixin from "../../mixins/localize-mixin"; -import "../../styles/polymer-ha-style"; - -/* - * @appliesMixin LocalizeMixin - */ -class HaChangePasswordCard extends LocalizeMixin(PolymerElement) { - static get template() { - return html` - -
- -
- - - - -
-
- - -
-
-
- `; - } - - static get properties() { - return { - hass: Object, - - _loading: { - type: Boolean, - value: false, - }, - - // Error message when can't talk to server etc - _statusMsg: String, - _errorMsg: String, - - _currentPassword: String, - _password1: String, - _password2: String, - }; - } - - ready() { - super.ready(); - this.addEventListener("keypress", (ev) => { - this._statusMsg = null; - if (ev.keyCode === 13) { - this._changePassword(); - } - }); - } - - async _changePassword() { - this._statusMsg = null; - if (!this._currentPassword || !this._password1 || !this._password2) return; - - if (this._password1 !== this._password2) { - this._errorMsg = "New password confirmation doesn't match"; - return; - } - - if (this._currentPassword === this._password1) { - this._errorMsg = "New password must be different than current password"; - return; - } - - this._loading = true; - this._errorMsg = null; - - try { - await this.hass.callWS({ - type: "config/auth_provider/homeassistant/change_password", - current_password: this._currentPassword, - new_password: this._password1, - }); - - this.setProperties({ - _statusMsg: "Password changed successfully", - _currentPassword: null, - _password1: null, - _password2: null, - }); - } catch (err) { - this._errorMsg = err.message; - } - - this._loading = false; - } -} - -customElements.define("ha-change-password-card", HaChangePasswordCard); diff --git a/src/panels/profile/ha-change-password-card.ts b/src/panels/profile/ha-change-password-card.ts new file mode 100644 index 0000000000..06c66fe3d4 --- /dev/null +++ b/src/panels/profile/ha-change-password-card.ts @@ -0,0 +1,195 @@ +import "@polymer/paper-input/paper-input"; +import "@polymer/paper-dialog/paper-dialog"; +import { + css, + CSSResult, + customElement, + html, + internalProperty, + LitElement, + property, + PropertyValues, + TemplateResult, +} from "lit-element"; +import "@material/mwc-button"; +import "../../components/ha-circular-progress"; +import "../../components/ha-card"; +import { haStyle } from "../../resources/styles"; +import type { HomeAssistant } from "../../types"; + +@customElement("ha-change-password-card") +class HaChangePasswordCard extends LitElement { + @property({ attribute: false }) public hass!: HomeAssistant; + + @internalProperty() private _loading = false; + + @internalProperty() private _statusMsg?: string; + + @internalProperty() private _errorMsg?: string; + + @internalProperty() private _currentPassword?: string; + + @internalProperty() private _password?: string; + + @internalProperty() private _passwordConfirm?: string; + + protected render(): TemplateResult { + return html` +
+ +
+ ${this._errorMsg + ? html`
${this._errorMsg}
` + : ""} + ${this._statusMsg + ? html`
${this._statusMsg}
` + : ""} + + + + ${this._currentPassword + ? html` + ` + : ""} +
+ +
+ ${this._loading + ? html`
+ +
` + : html`${this.hass.localize( + "ui.panel.profile.change_password.submit" + )}`} +
+
+
+ `; + } + + private _currentPasswordChanged(ev: CustomEvent) { + this._currentPassword = ev.detail.value; + } + + private _newPasswordChanged(ev: CustomEvent) { + this._password = ev.detail.value; + } + + private _newPasswordConfirmChanged(ev: CustomEvent) { + this._passwordConfirm = ev.detail.value; + } + + protected firstUpdated(changedProps: PropertyValues) { + super.firstUpdated(changedProps); + this.addEventListener("keypress", (ev) => { + this._statusMsg = undefined; + if (ev.keyCode === 13) { + this._changePassword(); + } + }); + } + + private async _changePassword() { + this._statusMsg = undefined; + if (!this._currentPassword || !this._password || !this._passwordConfirm) { + return; + } + + if (this._password !== this._passwordConfirm) { + this._errorMsg = this.hass.localize( + "ui.panel.profile.change_password.error_new_mismatch" + ); + return; + } + + if (this._currentPassword === this._password) { + this._errorMsg = this.hass.localize( + "ui.panel.profile.change_password.error_new_is_old" + ); + return; + } + + this._loading = true; + this._errorMsg = undefined; + + try { + await this.hass.callWS({ + type: "config/auth_provider/homeassistant/change_password", + current_password: this._currentPassword, + new_password: this._password, + }); + } catch (err) { + this._errorMsg = err.message; + return; + } finally { + this._loading = false; + } + + this._statusMsg = this.hass.localize( + "ui.panel.profile.change_password.success" + ); + this._currentPassword = undefined; + this._password = undefined; + this._passwordConfirm = undefined; + } + + static get styles(): CSSResult[] { + return [ + haStyle, + css` + .error { + color: var(--error-color); + } + .status { + color: var(--primary-color); + } + #currentPassword { + margin-top: -8px; + } + `, + ]; + } +} + +declare global { + interface HTMLElementTagNameMap { + "ha-change-password-card": HaChangePasswordCard; + } +} diff --git a/src/translations/en.json b/src/translations/en.json index 46a4aaf94b..68282dd703 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -2897,7 +2897,10 @@ "new_password": "New Password", "confirm_new_password": "Confirm New Password", "error_required": "Required", - "submit": "Submit" + "submit": "Submit", + "error_new_mismatch": "Entered new password values do not match", + "error_new_is_old": "New password must be different than current password", + "success": "Password changed successfully" }, "mfa": { "header": "Multi-factor Authentication Modules",