Adjust username handling in the cloud panel register and login flows (#22118)

* Use lowercase when registering

* Fallback to lowercase username if usernotfound is recieved

* Adjust resend

* handle reset password

* limit with else

* return early
This commit is contained in:
Joakim Sørensen 2024-09-27 12:31:48 +02:00 committed by GitHub
parent c721afa137
commit 468660d235
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 99 additions and 74 deletions

View File

@ -99,24 +99,32 @@ export class CloudForgotPassword extends LitElement {
this._requestInProgress = true; this._requestInProgress = true;
try { const doResetPassword = async (username: string) => {
await cloudForgotPassword(this.hass, email); try {
// @ts-ignore await cloudForgotPassword(this.hass, username);
fireEvent(this, "email-changed", { value: email }); // @ts-ignore
this._requestInProgress = false; fireEvent(this, "email-changed", { value: username });
// @ts-ignore this._requestInProgress = false;
fireEvent(this, "cloud-done", { // @ts-ignore
flashMessage: this.hass.localize( fireEvent(this, "cloud-done", {
"ui.panel.config.cloud.forgot_password.check_your_email" flashMessage: this.hass.localize(
), "ui.panel.config.cloud.forgot_password.check_your_email"
}); ),
} catch (err: any) { });
this._requestInProgress = false; } catch (err: any) {
this._error = this._requestInProgress = false;
err && err.body && err.body.message const errCode = err && err.body && err.body.code;
? err.body.message if (errCode === "usernotfound" && username !== username.toLowerCase()) {
: "Unknown error"; await doResetPassword(username.toLowerCase());
} } else {
this._error =
err && err.body && err.body.message
? err.body.message
: "Unknown error";
}
}
};
await doResetPassword(email);
} }
static get styles() { static get styles() {

View File

@ -227,53 +227,61 @@ export class CloudLogin extends LitElement {
this._requestInProgress = true; this._requestInProgress = true;
try { const doLogin = async (username: string) => {
const result = await cloudLogin(this.hass, email, password); try {
fireEvent(this, "ha-refresh-cloud-status"); const result = await cloudLogin(this.hass, username, password);
this.email = ""; fireEvent(this, "ha-refresh-cloud-status");
this._password = ""; this.email = "";
if (result.cloud_pipeline) { this._password = "";
if ( if (result.cloud_pipeline) {
await showConfirmationDialog(this, { if (
title: this.hass.localize( await showConfirmationDialog(this, {
"ui.panel.config.cloud.login.cloud_pipeline_title" title: this.hass.localize(
), "ui.panel.config.cloud.login.cloud_pipeline_title"
text: this.hass.localize( ),
"ui.panel.config.cloud.login.cloud_pipeline_text" text: this.hass.localize(
), "ui.panel.config.cloud.login.cloud_pipeline_text"
}) ),
) { })
setAssistPipelinePreferred(this.hass, result.cloud_pipeline); ) {
setAssistPipelinePreferred(this.hass, result.cloud_pipeline);
}
}
} catch (err: any) {
const errCode = err && err.body && err.body.code;
if (errCode === "PasswordChangeRequired") {
showAlertDialog(this, {
title: this.hass.localize(
"ui.panel.config.cloud.login.alert_password_change_required"
),
});
navigate("/config/cloud/forgot-password");
return;
}
if (errCode === "usernotfound" && username !== username.toLowerCase()) {
await doLogin(username.toLowerCase());
return;
} }
}
} catch (err: any) {
const errCode = err && err.body && err.body.code;
if (errCode === "PasswordChangeRequired") {
showAlertDialog(this, {
title: this.hass.localize(
"ui.panel.config.cloud.login.alert_password_change_required"
),
});
navigate("/config/cloud/forgot-password");
return;
}
this._password = ""; this._password = "";
this._requestInProgress = false; this._requestInProgress = false;
if (errCode === "UserNotConfirmed") { if (errCode === "UserNotConfirmed") {
this._error = this.hass.localize( this._error = this.hass.localize(
"ui.panel.config.cloud.login.alert_email_confirm_necessary" "ui.panel.config.cloud.login.alert_email_confirm_necessary"
); );
} else { } else {
this._error = this._error =
err && err.body && err.body.message err && err.body && err.body.message
? err.body.message ? err.body.message
: "Unknown error"; : "Unknown error";
}
emailField.focus();
} }
};
emailField.focus(); await doLogin(email);
}
} }
private _handleRegister() { private _handleRegister() {

View File

@ -197,9 +197,6 @@ export class CloudRegister extends LitElement {
const emailField = this._emailField; const emailField = this._emailField;
const passwordField = this._passwordField; const passwordField = this._passwordField;
const email = emailField.value;
const password = passwordField.value;
if (!emailField.reportValidity()) { if (!emailField.reportValidity()) {
passwordField.reportValidity(); passwordField.reportValidity();
emailField.focus(); emailField.focus();
@ -211,6 +208,9 @@ export class CloudRegister extends LitElement {
return; return;
} }
const email = emailField.value.toLowerCase();
const password = passwordField.value;
this._requestInProgress = true; this._requestInProgress = true;
try { try {
@ -229,22 +229,31 @@ export class CloudRegister extends LitElement {
private async _handleResendVerifyEmail() { private async _handleResendVerifyEmail() {
const emailField = this._emailField; const emailField = this._emailField;
const email = emailField.value;
if (!emailField.reportValidity()) { if (!emailField.reportValidity()) {
emailField.focus(); emailField.focus();
return; return;
} }
try { const email = emailField.value;
await cloudResendVerification(this.hass, email);
this._verificationEmailSent(email); const doResend = async (username: string) => {
} catch (err: any) { try {
this._error = await cloudResendVerification(this.hass, username);
err && err.body && err.body.message this._verificationEmailSent(username);
? err.body.message } catch (err: any) {
: "Unknown error"; const errCode = err && err.body && err.body.code;
} if (errCode === "usernotfound" && username !== username.toLowerCase()) {
await doResend(username.toLowerCase());
} else {
this._error =
err && err.body && err.body.message
? err.body.message
: "Unknown error";
}
}
};
await doResend(email);
} }
private _verificationEmailSent(email: string) { private _verificationEmailSent(email: string) {