Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 1d02e61

Browse files
authored
Improve taken username warning in registration for when request fails (#7621)
1 parent 68024c1 commit 1d02e61

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

src/components/views/auth/RegistrationForm.tsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ enum RegistrationField {
4444
PasswordConfirm = "field_password_confirm",
4545
}
4646

47+
enum UsernameAvailableStatus {
48+
Unknown,
49+
Available,
50+
Unavailable,
51+
Error,
52+
}
53+
4754
export const PASSWORD_MIN_SCORE = 3; // safely unguessable: moderate protection from offline slow-hash scenario.
4855

4956
interface IProps {
@@ -348,13 +355,25 @@ export default class RegistrationForm extends React.PureComponent<IProps, IState
348355
return result;
349356
};
350357

351-
private validateUsernameRules = withValidation({
358+
private validateUsernameRules = withValidation<this, UsernameAvailableStatus>({
352359
description: (_, results) => {
353360
// omit the description if the only failing result is the `available` one as it makes no sense for it.
354361
if (results.every(({ key, valid }) => key === "available" || valid)) return;
355362
return _t("Use lowercase letters, numbers, dashes and underscores only");
356363
},
357364
hideDescriptionIfValid: true,
365+
async deriveData(this: RegistrationForm, { value }) {
366+
if (!value) {
367+
return UsernameAvailableStatus.Unknown;
368+
}
369+
370+
try {
371+
const available = await this.props.matrixClient.isUsernameAvailable(value);
372+
return available ? UsernameAvailableStatus.Available : UsernameAvailableStatus.Unavailable;
373+
} catch (err) {
374+
return UsernameAvailableStatus.Error;
375+
}
376+
},
358377
rules: [
359378
{
360379
key: "required",
@@ -369,19 +388,16 @@ export default class RegistrationForm extends React.PureComponent<IProps, IState
369388
{
370389
key: "available",
371390
final: true,
372-
test: async ({ value }) => {
391+
test: async ({ value }, usernameAvailable) => {
373392
if (!value) {
374393
return true;
375394
}
376395

377-
try {
378-
await this.props.matrixClient.isUsernameAvailable(value);
379-
return true;
380-
} catch (err) {
381-
return false;
382-
}
396+
return usernameAvailable === UsernameAvailableStatus.Available;
383397
},
384-
invalid: () => _t("Someone already has that username. Try another or if it is you, sign in below."),
398+
invalid: (usernameAvailable) => usernameAvailable === UsernameAvailableStatus.Error
399+
? _t("Unable to check if username has been taken. Try again later.")
400+
: _t("Someone already has that username. Try another or if it is you, sign in below."),
385401
},
386402
],
387403
});

src/components/views/elements/Validation.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export default function withValidation<T = undefined, D = void>({
9292
}
9393

9494
const data = { value, allowEmpty };
95-
const derivedData = deriveData ? await deriveData(data) : undefined;
95+
const derivedData: D | undefined = deriveData ? await deriveData.call(this, data) : undefined;
9696

9797
const results: IResult[] = [];
9898
let valid = true;
@@ -106,13 +106,13 @@ export default function withValidation<T = undefined, D = void>({
106106
continue;
107107
}
108108

109-
if (rule.skip && rule.skip.call(this, data, derivedData)) {
109+
if (rule.skip?.call(this, data, derivedData)) {
110110
continue;
111111
}
112112

113113
// We're setting `this` to whichever component holds the validation
114114
// function. That allows rules to access the state of the component.
115-
const ruleValid = await rule.test.call(this, data, derivedData);
115+
const ruleValid: boolean = await rule.test.call(this, data, derivedData);
116116
valid = valid && ruleValid;
117117
if (ruleValid && rule.valid) {
118118
// If the rule's result is valid and has text to show for

src/i18n/strings/en_EN.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2939,6 +2939,7 @@
29392939
"Other users can invite you to rooms using your contact details": "Other users can invite you to rooms using your contact details",
29402940
"Enter phone number (required on this homeserver)": "Enter phone number (required on this homeserver)",
29412941
"Use lowercase letters, numbers, dashes and underscores only": "Use lowercase letters, numbers, dashes and underscores only",
2942+
"Unable to check if username has been taken. Try again later.": "Unable to check if username has been taken. Try again later.",
29422943
"Someone already has that username. Try another or if it is you, sign in below.": "Someone already has that username. Try another or if it is you, sign in below.",
29432944
"Phone (optional)": "Phone (optional)",
29442945
"Register": "Register",

0 commit comments

Comments
 (0)