Skip to content

Commit

Permalink
Show error when using wrong username format during onboarding (#22658)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkragten authored Nov 4, 2024
1 parent 01e33f5 commit efe90fc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/onboarding/onboarding-create-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import type { ValueChangedEvent } from "../types";
import { onBoardingStyles } from "./styles";
import { debounce } from "../common/util/debounce";

const CHECK_USERNAME_REGEX = /\s|[A-Z]/;

const CREATE_USER_SCHEMA: HaFormSchema[] = [
{
name: "name",
Expand Down Expand Up @@ -121,6 +123,7 @@ class OnboardingCreateUser extends LitElement {
ev: ValueChangedEvent<HaFormDataContainer>
): void {
const nameChanged = ev.detail.value.name !== this._newUser.name;
const usernameChanged = ev.detail.value.username !== this._newUser.username;
const passwordChanged =
ev.detail.value.password !== this._newUser.password ||
ev.detail.value.password_confirm !== this._newUser.password_confirm;
Expand All @@ -135,6 +138,9 @@ class OnboardingCreateUser extends LitElement {
this._debouncedCheckPasswordMatch();
}
}
if (usernameChanged) {
this._checkUsername();
}
}

private _debouncedCheckPasswordMatch = debounce(
Expand Down Expand Up @@ -164,6 +170,21 @@ class OnboardingCreateUser extends LitElement {
const parts = String(this._newUser.name).split(" ");
if (parts.length) {
this._newUser.username = parts[0].toLowerCase();
this._checkUsername();
}
}

private _checkUsername(): void {
const old = this._formError.username;
if (CHECK_USERNAME_REGEX.test(this._newUser.username as string)) {
this._formError.username = this.localize(
"ui.panel.page-onboarding.user.error.username_not_normalized"
);
} else {
this._formError.username = "";
}
if (old !== this._formError.username) {
this.requestUpdate("_formError");
}
}

Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7206,6 +7206,7 @@
},
"create_account": "Create account",
"error": {
"username_not_normalized": "Username can only contain lowercase letters, and can not contain whitespace.",
"password_not_match": "Passwords don't match"
}
},
Expand Down

0 comments on commit efe90fc

Please sign in to comment.