Skip to content
This repository was archived by the owner on May 18, 2025. It is now read-only.

Commit 7ed9559

Browse files
authoredDec 14, 2018
Merge pull request matrix-org#2351 from matrix-org/travis/fix-username-requirements
Standardize errors about localpart structure
2 parents bae9996 + 576bfed commit 7ed9559

File tree

6 files changed

+17
-16
lines changed

6 files changed

+17
-16
lines changed
 

‎src/Registration.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import MatrixClientPeg from './MatrixClientPeg';
2626
import Modal from './Modal';
2727
import { _t } from './languageHandler';
2828

29+
// Regex for what a "safe" or "Matrix-looking" localpart would be.
30+
// TODO: Update as needed for https://github.com/matrix-org/matrix-doc/issues/1514
31+
export const SAFE_LOCALPART_REGEX = /^[a-z0-9=_\-./]+$/;
32+
2933
/**
3034
* Starts either the ILAG or full registration flow, depending
3135
* on what the HS supports

‎src/components/structures/login/Registration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ module.exports = React.createClass({
342342
errMsg = _t('A phone number is required to register on this homeserver.');
343343
break;
344344
case "RegistrationForm.ERR_USERNAME_INVALID":
345-
errMsg = _t('User names may only contain letters, numbers, dots, hyphens and underscores.');
345+
errMsg = _t("Only use lower case letters, numbers and '=_-./'");
346346
break;
347347
case "RegistrationForm.ERR_USERNAME_BLANK":
348348
errMsg = _t('You need to enter a user name.');

‎src/components/views/dialogs/SetMxIdDialog.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import MatrixClientPeg from '../../../MatrixClientPeg';
2323
import classnames from 'classnames';
2424
import { KeyCode } from '../../../Keyboard';
2525
import { _t } from '../../../languageHandler';
26+
import { SAFE_LOCALPART_REGEX } from '../../../Registration';
2627

2728
// The amount of time to wait for further changes to the input username before
2829
// sending a request to the server
@@ -110,12 +111,11 @@ export default React.createClass({
110111
},
111112

112113
_doUsernameCheck: function() {
113-
// XXX: SPEC-1
114-
// Check if username is valid
115-
// Naive impl copied from https://github.com/matrix-org/matrix-react-sdk/blob/66c3a6d9ca695780eb6b662e242e88323053ff33/src/components/views/login/RegistrationForm.js#L190
116-
if (encodeURIComponent(this.state.username) !== this.state.username) {
114+
// We do a quick check ahead of the username availability API to ensure the
115+
// user ID roughly looks okay from a Matrix perspective.
116+
if (!SAFE_LOCALPART_REGEX.test(this.state.username)) {
117117
this.setState({
118-
usernameError: _t('User names may only contain letters, numbers, dots, hyphens and underscores.'),
118+
usernameError: _t("Only use lower case letters, numbers and '=_-./'"),
119119
});
120120
return Promise.reject();
121121
}
@@ -210,7 +210,6 @@ export default React.createClass({
210210
render: function() {
211211
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
212212
const InteractiveAuth = sdk.getComponent('structures.InteractiveAuth');
213-
const Spinner = sdk.getComponent('elements.Spinner');
214213

215214
let auth;
216215
if (this.state.doingUIAuth) {
@@ -230,9 +229,8 @@ export default React.createClass({
230229
});
231230

232231
let usernameIndicator = null;
233-
let usernameBusyIndicator = null;
234232
if (this.state.usernameBusy) {
235-
usernameBusyIndicator = <Spinner w="24" h="24" />;
233+
usernameIndicator = <div>{_t("Checking...")}</div>;
236234
} else {
237235
const usernameAvailable = this.state.username &&
238236
this.state.usernameCheckSupport && !this.state.usernameError;
@@ -270,7 +268,6 @@ export default React.createClass({
270268
size="30"
271269
className={inputClasses}
272270
/>
273-
{ usernameBusyIndicator }
274271
</div>
275272
{ usernameIndicator }
276273
<p>

‎src/components/views/login/RegistrationForm.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { looksValid as phoneNumberLooksValid } from '../../../phonenumber';
2525
import Modal from '../../../Modal';
2626
import { _t } from '../../../languageHandler';
2727
import SdkConfig from '../../../SdkConfig';
28-
import SettingsStore from "../../../settings/SettingsStore";
28+
import { SAFE_LOCALPART_REGEX } from '../../../Registration';
2929

3030
const FIELD_EMAIL = 'field_email';
3131
const FIELD_PHONE_COUNTRY = 'field_phone_country';
@@ -194,9 +194,8 @@ module.exports = React.createClass({
194194
} else this.markFieldValid(field_id, phoneNumberValid, "RegistrationForm.ERR_PHONE_NUMBER_INVALID");
195195
break;
196196
case FIELD_USERNAME:
197-
// XXX: SPEC-1
198-
var username = this.refs.username.value.trim();
199-
if (encodeURIComponent(username) != username) {
197+
const username = this.refs.username.value.trim();
198+
if (!SAFE_LOCALPART_REGEX.test(username)) {
200199
this.markFieldValid(
201200
field_id,
202201
false,

‎src/components/views/room_settings/AliasSettings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ module.exports = React.createClass({
130130
},
131131

132132
isAliasValid: function(alias) {
133-
// XXX: FIXME SPEC-1
133+
// XXX: FIXME https://github.com/matrix-org/matrix-doc/issues/668
134134
return (alias.match(/^#([^\/:,]+?):(.+)$/) && encodeURI(alias) === alias);
135135
},
136136

‎src/i18n/strings/en_EN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,10 +985,11 @@
985985
"Unable to verify email address.": "Unable to verify email address.",
986986
"This will allow you to reset your password and receive notifications.": "This will allow you to reset your password and receive notifications.",
987987
"Skip": "Skip",
988-
"User names may only contain letters, numbers, dots, hyphens and underscores.": "User names may only contain letters, numbers, dots, hyphens and underscores.",
988+
"Only use lower case letters, numbers and '=_-./'": "Only use lower case letters, numbers and '=_-./'",
989989
"Username not available": "Username not available",
990990
"Username invalid: %(errMessage)s": "Username invalid: %(errMessage)s",
991991
"An error occurred: %(error_string)s": "An error occurred: %(error_string)s",
992+
"Checking...": "Checking...",
992993
"Username available": "Username available",
993994
"To get started, please pick a username!": "To get started, please pick a username!",
994995
"This will be your account name on the <span></span> homeserver, or you can pick a <a>different server</a>.": "This will be your account name on the <span></span> homeserver, or you can pick a <a>different server</a>.",

0 commit comments

Comments
 (0)