|
| 1 | +import { LitElement, css, html } from "lit"; |
| 2 | +import { customElement, property, query, state } from "lit/decorators"; |
| 3 | +import { fireEvent } from "../../../common/dom/fire_event"; |
| 4 | +import { navigate } from "../../../common/navigate"; |
| 5 | +import "../../../components/ha-alert"; |
| 6 | +import "../../../components/ha-button"; |
| 7 | +import "../../../components/ha-password-field"; |
| 8 | +import type { HaPasswordField } from "../../../components/ha-password-field"; |
| 9 | +import "../../../components/ha-svg-icon"; |
| 10 | +import "../../../components/ha-textfield"; |
| 11 | +import type { HaTextField } from "../../../components/ha-textfield"; |
| 12 | +import { cloudLogin } from "../../../data/cloud"; |
| 13 | +import type { HomeAssistant } from "../../../types"; |
| 14 | +import { showAlertDialog } from "../../generic/show-dialog-box"; |
| 15 | +import { AssistantSetupStyles } from "../styles"; |
| 16 | + |
| 17 | +@customElement("cloud-step-signin") |
| 18 | +export class CloudStepSignin extends LitElement { |
| 19 | + @property({ attribute: false }) public hass!: HomeAssistant; |
| 20 | + |
| 21 | + @state() private _requestInProgress = false; |
| 22 | + |
| 23 | + @state() private _error?: string; |
| 24 | + |
| 25 | + @query("#email", true) private _emailField!: HaTextField; |
| 26 | + |
| 27 | + @query("#password", true) private _passwordField!: HaPasswordField; |
| 28 | + |
| 29 | + render() { |
| 30 | + return html`<div class="content"> |
| 31 | + <img |
| 32 | + src=${`/static/images/logo_nabu_casa${this.hass.themes?.darkMode ? "_dark" : ""}.png`} |
| 33 | + alt="Nabu Casa logo" |
| 34 | + /> |
| 35 | + <h1>Sign in</h1> |
| 36 | + ${this._error |
| 37 | + ? html`<ha-alert alert-type="error">${this._error}</ha-alert>` |
| 38 | + : ""} |
| 39 | + <ha-textfield |
| 40 | + autofocus |
| 41 | + id="email" |
| 42 | + name="email" |
| 43 | + .label=${this.hass.localize( |
| 44 | + "ui.panel.config.cloud.register.email_address" |
| 45 | + )} |
| 46 | + .disabled=${this._requestInProgress} |
| 47 | + type="email" |
| 48 | + autocomplete="email" |
| 49 | + required |
| 50 | + @keydown=${this._keyDown} |
| 51 | + validationMessage=${this.hass.localize( |
| 52 | + "ui.panel.config.cloud.register.email_error_msg" |
| 53 | + )} |
| 54 | + ></ha-textfield> |
| 55 | + <ha-password-field |
| 56 | + id="password" |
| 57 | + name="password" |
| 58 | + .label=${this.hass.localize( |
| 59 | + "ui.panel.config.cloud.register.password" |
| 60 | + )} |
| 61 | + .disabled=${this._requestInProgress} |
| 62 | + autocomplete="new-password" |
| 63 | + minlength="8" |
| 64 | + required |
| 65 | + @keydown=${this._keyDown} |
| 66 | + validationMessage=${this.hass.localize( |
| 67 | + "ui.panel.config.cloud.register.password_error_msg" |
| 68 | + )} |
| 69 | + ></ha-password-field> |
| 70 | + </div> |
| 71 | + <div class="footer"> |
| 72 | + <ha-button |
| 73 | + unelevated |
| 74 | + @click=${this._handleLogin} |
| 75 | + .disabled=${this._requestInProgress} |
| 76 | + >Sign in</ha-button |
| 77 | + > |
| 78 | + </div>`; |
| 79 | + } |
| 80 | + |
| 81 | + private _keyDown(ev: KeyboardEvent) { |
| 82 | + if (ev.key === "Enter") { |
| 83 | + this._handleLogin(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private async _handleLogin() { |
| 88 | + const emailField = this._emailField; |
| 89 | + const passwordField = this._passwordField; |
| 90 | + |
| 91 | + const email = emailField.value; |
| 92 | + const password = passwordField.value; |
| 93 | + |
| 94 | + if (!emailField.reportValidity()) { |
| 95 | + passwordField.reportValidity(); |
| 96 | + emailField.focus(); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + if (!passwordField.reportValidity()) { |
| 101 | + passwordField.focus(); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + this._requestInProgress = true; |
| 106 | + |
| 107 | + const doLogin = async (username: string) => { |
| 108 | + try { |
| 109 | + await cloudLogin(this.hass, username, password); |
| 110 | + } catch (err: any) { |
| 111 | + const errCode = err && err.body && err.body.code; |
| 112 | + |
| 113 | + if (errCode === "usernotfound" && username !== username.toLowerCase()) { |
| 114 | + await doLogin(username.toLowerCase()); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + if (errCode === "PasswordChangeRequired") { |
| 119 | + showAlertDialog(this, { |
| 120 | + title: this.hass.localize( |
| 121 | + "ui.panel.config.cloud.login.alert_password_change_required" |
| 122 | + ), |
| 123 | + }); |
| 124 | + navigate("/config/cloud/forgot-password"); |
| 125 | + fireEvent(this, "closed"); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + this._requestInProgress = false; |
| 130 | + |
| 131 | + if (errCode === "UserNotConfirmed") { |
| 132 | + this._error = this.hass.localize( |
| 133 | + "ui.panel.config.cloud.login.alert_email_confirm_necessary" |
| 134 | + ); |
| 135 | + } else { |
| 136 | + this._error = |
| 137 | + err && err.body && err.body.message |
| 138 | + ? err.body.message |
| 139 | + : "Unknown error"; |
| 140 | + } |
| 141 | + |
| 142 | + emailField.focus(); |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + await doLogin(email); |
| 147 | + } |
| 148 | + |
| 149 | + static styles = [ |
| 150 | + AssistantSetupStyles, |
| 151 | + css` |
| 152 | + :host { |
| 153 | + display: block; |
| 154 | + } |
| 155 | + ha-textfield, |
| 156 | + ha-password-field { |
| 157 | + display: block; |
| 158 | + } |
| 159 | + `, |
| 160 | + ]; |
| 161 | +} |
| 162 | + |
| 163 | +declare global { |
| 164 | + interface HTMLElementTagNameMap { |
| 165 | + "cloud-step-signin": CloudStepSignin; |
| 166 | + } |
| 167 | +} |
0 commit comments