2015-11-30 21:11:04 +03:00
|
|
|
/*
|
2019-08-09 20:08:17 +03:00
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
2020-11-19 18:10:40 +03:00
|
|
|
Copyright 2015, 2016, 2017, 2018, 2019, 2020 The Matrix.org Foundation C.I.C.
|
2015-11-30 21:11:04 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-11-20 16:34:44 +03:00
|
|
|
import React from 'react';
|
2020-11-19 18:10:40 +03:00
|
|
|
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from '../../../index';
|
|
|
|
import * as Email from '../../../email';
|
2017-03-14 14:50:13 +03:00
|
|
|
import { looksValid as phoneNumberLooksValid } from '../../../phonenumber';
|
2017-02-24 14:41:23 +03:00
|
|
|
import Modal from '../../../Modal';
|
2017-05-30 17:09:57 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2017-11-12 02:46:43 +03:00
|
|
|
import SdkConfig from '../../../SdkConfig';
|
2018-12-14 02:05:34 +03:00
|
|
|
import { SAFE_LOCALPART_REGEX } from '../../../Registration';
|
2019-04-16 18:52:31 +03:00
|
|
|
import withValidation from '../elements/Validation';
|
2019-05-03 08:02:01 +03:00
|
|
|
import {ValidatedServerConfig} from "../../../utils/AutoDiscoveryUtils";
|
2020-05-14 22:19:15 +03:00
|
|
|
import PassphraseField from "./PassphraseField";
|
2020-10-29 18:53:14 +03:00
|
|
|
import CountlyAnalytics from "../../../CountlyAnalytics";
|
2020-11-23 13:25:46 +03:00
|
|
|
import Field from '../elements/Field';
|
|
|
|
import RegistrationEmailPromptDialog from '../dialogs/RegistrationEmailPromptDialog';
|
|
|
|
import QuestionDialog from '../dialogs/QuestionDialog';
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
enum RegistrationField {
|
|
|
|
Email = "field_email",
|
|
|
|
PhoneNumber = "field_phone_number",
|
|
|
|
Username = "field_username",
|
|
|
|
Password = "field_password",
|
|
|
|
PasswordConfirm = "field_password_confirm",
|
|
|
|
}
|
2016-01-15 16:31:41 +03:00
|
|
|
|
2019-05-07 14:35:42 +03:00
|
|
|
const PASSWORD_MIN_SCORE = 3; // safely unguessable: moderate protection from offline slow-hash scenario.
|
2019-04-23 16:55:57 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
interface IProps {
|
|
|
|
// Values pre-filled in the input boxes when the component loads
|
|
|
|
defaultEmail?: string;
|
|
|
|
defaultPhoneCountry?: string;
|
|
|
|
defaultPhoneNumber?: string;
|
|
|
|
defaultUsername?: string;
|
|
|
|
defaultPassword?: string;
|
|
|
|
flows: {
|
|
|
|
stages: string[];
|
|
|
|
}[];
|
|
|
|
serverConfig: ValidatedServerConfig;
|
|
|
|
canSubmit?: boolean;
|
|
|
|
serverRequiresIdServer?: boolean;
|
|
|
|
|
|
|
|
onRegisterClick(params: {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
email?: string;
|
|
|
|
phoneCountry?: string;
|
|
|
|
phoneNumber?: string;
|
|
|
|
}): Promise<void>;
|
|
|
|
onEditServerDetailsClick?(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
// Field error codes by field ID
|
|
|
|
fieldValid: Partial<Record<RegistrationField, boolean>>;
|
|
|
|
// The ISO2 country code selected in the phone number entry
|
|
|
|
phoneCountry: string;
|
|
|
|
username: string;
|
|
|
|
email: string;
|
|
|
|
phoneNumber: string;
|
|
|
|
password: string;
|
|
|
|
passwordConfirm: string;
|
|
|
|
passwordComplexity?: number;
|
|
|
|
}
|
|
|
|
|
2020-08-29 14:57:11 +03:00
|
|
|
/*
|
2015-11-30 21:11:04 +03:00
|
|
|
* A pure UI component which displays a registration form.
|
|
|
|
*/
|
2020-11-19 18:10:40 +03:00
|
|
|
export default class RegistrationForm extends React.PureComponent<IProps, IState> {
|
2020-08-29 14:14:16 +03:00
|
|
|
static defaultProps = {
|
|
|
|
onValidationChange: console.error,
|
|
|
|
canSubmit: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
this.state = {
|
2019-04-17 13:39:11 +03:00
|
|
|
fieldValid: {},
|
2017-03-14 14:50:13 +03:00
|
|
|
phoneCountry: this.props.defaultPhoneCountry,
|
2019-08-09 20:08:17 +03:00
|
|
|
username: this.props.defaultUsername || "",
|
|
|
|
email: this.props.defaultEmail || "",
|
|
|
|
phoneNumber: this.props.defaultPhoneNumber || "",
|
|
|
|
password: this.props.defaultPassword || "",
|
2020-05-01 13:03:48 +03:00
|
|
|
passwordConfirm: this.props.defaultPassword || "",
|
2019-04-23 16:55:57 +03:00
|
|
|
passwordComplexity: null,
|
2015-11-30 21:11:04 +03:00
|
|
|
};
|
2020-10-29 18:53:14 +03:00
|
|
|
|
|
|
|
CountlyAnalytics.instance.track("onboarding_registration_begin");
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private onSubmit = async ev => {
|
2015-11-30 21:11:04 +03:00
|
|
|
ev.preventDefault();
|
2020-11-23 13:25:46 +03:00
|
|
|
ev.persist();
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2019-06-05 08:41:59 +03:00
|
|
|
if (!this.props.canSubmit) return;
|
|
|
|
|
2019-04-23 16:55:57 +03:00
|
|
|
const allFieldsValid = await this.verifyFieldsBeforeSubmit();
|
2019-04-17 16:23:59 +03:00
|
|
|
if (!allFieldsValid) {
|
2020-10-29 18:53:14 +03:00
|
|
|
CountlyAnalytics.instance.track("onboarding_registration_submit_failed");
|
2019-04-17 16:23:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-09 20:08:17 +03:00
|
|
|
if (this.state.email === '') {
|
2019-08-07 13:15:56 +03:00
|
|
|
const haveIs = Boolean(this.props.serverConfig.isUrl);
|
|
|
|
|
2019-10-28 17:13:24 +03:00
|
|
|
if (this.props.serverRequiresIdServer && !haveIs) {
|
2020-11-23 13:25:46 +03:00
|
|
|
Modal.createTrackedDialog("No identity server no email", '', QuestionDialog, {
|
|
|
|
title: _t("Warning!"),
|
|
|
|
description: _t(
|
|
|
|
"No identity server is configured so you cannot add an email address in order to " +
|
|
|
|
"reset your password in the future.",
|
|
|
|
),
|
|
|
|
button: _t("Continue"),
|
|
|
|
onFinished: async (confirmed) => {
|
|
|
|
if (confirmed) this.doSubmit(ev);
|
|
|
|
},
|
|
|
|
});
|
2020-11-19 18:10:40 +03:00
|
|
|
} else if (this.showEmail()) {
|
2020-11-23 13:25:46 +03:00
|
|
|
CountlyAnalytics.instance.track("onboarding_registration_submit_warn");
|
|
|
|
Modal.createTrackedDialog("Email prompt dialog", '', RegistrationEmailPromptDialog, {
|
|
|
|
onFinished: async (confirmed: boolean, email?: string) => {
|
|
|
|
if (confirmed) {
|
|
|
|
this.setState({
|
|
|
|
email,
|
|
|
|
}, () => {
|
|
|
|
this.doSubmit(ev);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2020-04-22 13:33:45 +03:00
|
|
|
} else {
|
|
|
|
// user can't set an e-mail so don't prompt them to
|
2020-11-19 18:10:40 +03:00
|
|
|
this.doSubmit(ev);
|
2020-04-22 13:33:45 +03:00
|
|
|
return;
|
2019-08-07 13:15:56 +03:00
|
|
|
}
|
2019-04-17 16:23:59 +03:00
|
|
|
} else {
|
2020-11-19 18:10:40 +03:00
|
|
|
this.doSubmit(ev);
|
2015-11-30 21:11:04 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private doSubmit(ev) {
|
2019-03-14 17:29:04 +03:00
|
|
|
const email = this.state.email.trim();
|
2020-10-29 18:53:14 +03:00
|
|
|
|
|
|
|
CountlyAnalytics.instance.track("onboarding_registration_submit_ok", {
|
|
|
|
email: !!email,
|
|
|
|
});
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const promise = this.props.onRegisterClick({
|
2019-03-14 17:29:04 +03:00
|
|
|
username: this.state.username.trim(),
|
|
|
|
password: this.state.password.trim(),
|
2017-01-19 12:51:40 +03:00
|
|
|
email: email,
|
2017-03-14 14:50:13 +03:00
|
|
|
phoneCountry: this.state.phoneCountry,
|
2019-03-14 17:29:04 +03:00
|
|
|
phoneNumber: this.state.phoneNumber,
|
2016-03-21 04:15:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (promise) {
|
|
|
|
ev.target.disabled = true;
|
|
|
|
promise.finally(function() {
|
|
|
|
ev.target.disabled = false;
|
|
|
|
});
|
2016-08-03 17:59:17 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-03-21 04:15:11 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private async verifyFieldsBeforeSubmit() {
|
2019-04-25 13:27:03 +03:00
|
|
|
// Blur the active element if any, so we first run its blur validation,
|
|
|
|
// which is less strict than the pass we're about to do below for all fields.
|
2020-11-19 18:10:40 +03:00
|
|
|
const activeElement = document.activeElement as HTMLElement;
|
2019-04-25 13:27:03 +03:00
|
|
|
if (activeElement) {
|
|
|
|
activeElement.blur();
|
|
|
|
}
|
|
|
|
|
2019-04-18 23:33:37 +03:00
|
|
|
const fieldIDsInDisplayOrder = [
|
2020-11-19 18:10:40 +03:00
|
|
|
RegistrationField.Username,
|
|
|
|
RegistrationField.Password,
|
|
|
|
RegistrationField.PasswordConfirm,
|
|
|
|
RegistrationField.Email,
|
|
|
|
RegistrationField.PhoneNumber,
|
2019-04-18 23:33:37 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
// Run all fields with stricter validation that no longer allows empty
|
|
|
|
// values for required fields.
|
|
|
|
for (const fieldID of fieldIDsInDisplayOrder) {
|
|
|
|
const field = this[fieldID];
|
|
|
|
if (!field) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-13 16:17:09 +03:00
|
|
|
// We must wait for these validations to finish before queueing
|
2019-05-13 16:24:56 +03:00
|
|
|
// up the setState below so our setState goes in the queue after
|
2019-05-13 16:17:09 +03:00
|
|
|
// all the setStates from these validate calls (that's how we
|
|
|
|
// know they've finished).
|
|
|
|
await field.validate({ allowEmpty: false });
|
2019-04-18 23:33:37 +03:00
|
|
|
}
|
|
|
|
|
2019-04-23 16:55:57 +03:00
|
|
|
// Validation and state updates are async, so we need to wait for them to complete
|
|
|
|
// first. Queue a `setState` callback and wait for it to resolve.
|
|
|
|
await new Promise(resolve => this.setState({}, resolve));
|
|
|
|
|
2019-04-18 23:33:37 +03:00
|
|
|
if (this.allFieldsValid()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const invalidField = this.findFirstInvalidField(fieldIDsInDisplayOrder);
|
2019-04-17 16:23:59 +03:00
|
|
|
|
|
|
|
if (!invalidField) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-18 23:33:37 +03:00
|
|
|
// Focus the first invalid field and show feedback in the stricter mode
|
|
|
|
// that no longer allows empty values for required fields.
|
2019-04-17 16:23:59 +03:00
|
|
|
invalidField.focus();
|
2019-04-18 23:33:37 +03:00
|
|
|
invalidField.validate({ allowEmpty: false, focused: true });
|
2019-04-17 16:23:59 +03:00
|
|
|
return false;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-04-17 16:23:59 +03:00
|
|
|
|
2016-01-15 19:17:27 +03:00
|
|
|
/**
|
2019-01-24 03:32:36 +03:00
|
|
|
* @returns {boolean} true if all fields were valid last time they were validated.
|
2016-01-15 19:17:27 +03:00
|
|
|
*/
|
2020-11-19 18:10:40 +03:00
|
|
|
private allFieldsValid() {
|
2019-04-19 01:29:05 +03:00
|
|
|
const keys = Object.keys(this.state.fieldValid);
|
2019-04-17 13:39:11 +03:00
|
|
|
for (let i = 0; i < keys.length; ++i) {
|
|
|
|
if (!this.state.fieldValid[keys[i]]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-01-15 19:17:27 +03:00
|
|
|
return true;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-01-15 19:17:27 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private findFirstInvalidField(fieldIDs: RegistrationField[]) {
|
2019-04-17 16:23:59 +03:00
|
|
|
for (const fieldID of fieldIDs) {
|
|
|
|
if (!this.state.fieldValid[fieldID] && this[fieldID]) {
|
|
|
|
return this[fieldID];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-04-17 16:23:59 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private markFieldValid(fieldID: RegistrationField, valid: boolean) {
|
2019-04-17 13:39:11 +03:00
|
|
|
const { fieldValid } = this.state;
|
|
|
|
fieldValid[fieldID] = valid;
|
|
|
|
this.setState({
|
|
|
|
fieldValid,
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-04-17 13:39:11 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private onEmailChange = ev => {
|
2019-03-14 17:29:04 +03:00
|
|
|
this.setState({
|
|
|
|
email: ev.target.value,
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-03-14 17:29:04 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private onEmailValidate = async fieldState => {
|
2020-08-29 20:28:15 +03:00
|
|
|
const result = await this.validateEmailRules(fieldState);
|
2020-11-19 18:10:40 +03:00
|
|
|
this.markFieldValid(RegistrationField.Email, result.valid);
|
2019-04-19 00:53:46 +03:00
|
|
|
return result;
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-04-19 00:53:46 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private validateEmailRules = withValidation({
|
2019-04-19 00:53:46 +03:00
|
|
|
description: () => _t("Use an email address to recover your account"),
|
2020-11-09 04:07:15 +03:00
|
|
|
hideDescriptionIfValid: true,
|
2019-04-19 00:53:46 +03:00
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
key: "required",
|
2020-11-19 18:10:40 +03:00
|
|
|
test(this: RegistrationForm, { value, allowEmpty }) {
|
|
|
|
return allowEmpty || !this.authStepIsRequired('m.login.email.identity') || !!value;
|
2019-04-19 00:53:46 +03:00
|
|
|
},
|
|
|
|
invalid: () => _t("Enter email address (required on this homeserver)"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "email",
|
|
|
|
test: ({ value }) => !value || Email.looksValid(value),
|
|
|
|
invalid: () => _t("Doesn't look like a valid email address"),
|
|
|
|
},
|
|
|
|
],
|
2020-08-29 14:14:16 +03:00
|
|
|
});
|
2019-04-19 00:53:46 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private onPasswordChange = ev => {
|
2019-03-14 17:29:04 +03:00
|
|
|
this.setState({
|
|
|
|
password: ev.target.value,
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-03-14 17:29:04 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private onPasswordValidate = result => {
|
|
|
|
this.markFieldValid(RegistrationField.Password, result.valid);
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-01-30 23:48:12 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private onPasswordConfirmChange = ev => {
|
2019-03-14 17:29:04 +03:00
|
|
|
this.setState({
|
|
|
|
passwordConfirm: ev.target.value,
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-03-14 17:29:04 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private onPasswordConfirmValidate = async fieldState => {
|
2020-08-29 20:28:15 +03:00
|
|
|
const result = await this.validatePasswordConfirmRules(fieldState);
|
2020-11-19 18:10:40 +03:00
|
|
|
this.markFieldValid(RegistrationField.PasswordConfirm, result.valid);
|
2019-04-19 01:29:05 +03:00
|
|
|
return result;
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-04-19 01:29:05 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private validatePasswordConfirmRules = withValidation({
|
2019-04-19 01:29:05 +03:00
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
key: "required",
|
|
|
|
test: ({ value, allowEmpty }) => allowEmpty || !!value,
|
|
|
|
invalid: () => _t("Confirm password"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "match",
|
2020-11-19 18:10:40 +03:00
|
|
|
test(this: RegistrationForm, { value }) {
|
2019-04-19 01:29:05 +03:00
|
|
|
return !value || value === this.state.password;
|
|
|
|
},
|
|
|
|
invalid: () => _t("Passwords don't match"),
|
|
|
|
},
|
2020-11-19 18:10:40 +03:00
|
|
|
],
|
2020-08-29 14:14:16 +03:00
|
|
|
});
|
2019-04-19 01:29:05 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private onPhoneCountryChange = newVal => {
|
2017-03-14 14:50:13 +03:00
|
|
|
this.setState({
|
2017-04-25 13:21:47 +03:00
|
|
|
phoneCountry: newVal.iso2,
|
2017-03-14 14:50:13 +03:00
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-03-14 14:50:13 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private onPhoneNumberChange = ev => {
|
2019-03-14 17:29:04 +03:00
|
|
|
this.setState({
|
|
|
|
phoneNumber: ev.target.value,
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-03-14 17:29:04 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private onPhoneNumberValidate = async fieldState => {
|
2020-08-29 20:28:15 +03:00
|
|
|
const result = await this.validatePhoneNumberRules(fieldState);
|
2020-11-19 18:10:40 +03:00
|
|
|
this.markFieldValid(RegistrationField.PhoneNumber, result.valid);
|
2019-04-19 01:05:55 +03:00
|
|
|
return result;
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-04-19 01:05:55 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private validatePhoneNumberRules = withValidation({
|
2019-04-19 01:05:55 +03:00
|
|
|
description: () => _t("Other users can invite you to rooms using your contact details"),
|
2020-11-09 04:07:15 +03:00
|
|
|
hideDescriptionIfValid: true,
|
2019-04-19 01:05:55 +03:00
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
key: "required",
|
2020-11-19 18:10:40 +03:00
|
|
|
test(this: RegistrationForm, { value, allowEmpty }) {
|
|
|
|
return allowEmpty || !this.authStepIsRequired('m.login.msisdn') || !!value;
|
2019-04-19 01:05:55 +03:00
|
|
|
},
|
|
|
|
invalid: () => _t("Enter phone number (required on this homeserver)"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "email",
|
|
|
|
test: ({ value }) => !value || phoneNumberLooksValid(value),
|
|
|
|
invalid: () => _t("Doesn't look like a valid phone number"),
|
|
|
|
},
|
|
|
|
],
|
2020-08-29 14:14:16 +03:00
|
|
|
});
|
2019-04-19 01:05:55 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private onUsernameChange = ev => {
|
2019-03-14 17:29:04 +03:00
|
|
|
this.setState({
|
|
|
|
username: ev.target.value,
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-03-14 17:29:04 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private onUsernameValidate = async fieldState => {
|
2020-08-29 20:28:15 +03:00
|
|
|
const result = await this.validateUsernameRules(fieldState);
|
2020-11-19 18:10:40 +03:00
|
|
|
this.markFieldValid(RegistrationField.Username, result.valid);
|
2019-04-17 13:39:11 +03:00
|
|
|
return result;
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-04-17 13:39:11 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private validateUsernameRules = withValidation({
|
2019-06-07 15:57:28 +03:00
|
|
|
description: () => _t("Use lowercase letters, numbers, dashes and underscores only"),
|
2020-11-09 04:07:15 +03:00
|
|
|
hideDescriptionIfValid: true,
|
2019-04-17 13:39:11 +03:00
|
|
|
rules: [
|
2019-04-18 23:33:37 +03:00
|
|
|
{
|
|
|
|
key: "required",
|
|
|
|
test: ({ value, allowEmpty }) => allowEmpty || !!value,
|
|
|
|
invalid: () => _t("Enter username"),
|
|
|
|
},
|
2019-04-17 13:39:11 +03:00
|
|
|
{
|
|
|
|
key: "safeLocalpart",
|
2019-04-18 23:33:37 +03:00
|
|
|
test: ({ value }) => !value || SAFE_LOCALPART_REGEX.test(value),
|
2019-04-17 13:39:11 +03:00
|
|
|
invalid: () => _t("Some characters not allowed"),
|
|
|
|
},
|
|
|
|
],
|
2020-08-29 14:14:16 +03:00
|
|
|
});
|
2019-04-17 13:39:11 +03:00
|
|
|
|
2019-02-01 01:37:37 +03:00
|
|
|
/**
|
|
|
|
* A step is required if all flows include that step.
|
|
|
|
*
|
|
|
|
* @param {string} step A stage name to check
|
|
|
|
* @returns {boolean} Whether it is required
|
|
|
|
*/
|
2020-11-19 18:10:40 +03:00
|
|
|
private authStepIsRequired(step: string) {
|
2019-02-01 01:37:37 +03:00
|
|
|
return this.props.flows.every((flow) => {
|
|
|
|
return flow.stages.includes(step);
|
2018-09-04 20:26:09 +03:00
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2018-09-04 20:26:09 +03:00
|
|
|
|
2019-02-01 03:07:40 +03:00
|
|
|
/**
|
|
|
|
* A step is used if any flows include that step.
|
|
|
|
*
|
|
|
|
* @param {string} step A stage name to check
|
|
|
|
* @returns {boolean} Whether it is used
|
|
|
|
*/
|
2020-11-19 18:10:40 +03:00
|
|
|
private authStepIsUsed(step: string) {
|
2019-02-01 03:07:40 +03:00
|
|
|
return this.props.flows.some((flow) => {
|
|
|
|
return flow.stages.includes(step);
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-02-01 03:07:40 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private showEmail() {
|
2019-08-07 13:15:56 +03:00
|
|
|
const haveIs = Boolean(this.props.serverConfig.isUrl);
|
2019-10-28 17:13:24 +03:00
|
|
|
if (
|
|
|
|
(this.props.serverRequiresIdServer && !haveIs) ||
|
2020-11-19 18:10:40 +03:00
|
|
|
!this.authStepIsUsed('m.login.email.identity')
|
2019-10-28 17:13:24 +03:00
|
|
|
) {
|
2019-08-07 13:15:56 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-08-07 13:15:56 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private showPhoneNumber() {
|
2019-08-28 17:34:50 +03:00
|
|
|
const threePidLogin = !SdkConfig.get().disable_3pid_login;
|
|
|
|
const haveIs = Boolean(this.props.serverConfig.isUrl);
|
2019-10-28 17:13:24 +03:00
|
|
|
if (
|
|
|
|
!threePidLogin ||
|
|
|
|
(this.props.serverRequiresIdServer && !haveIs) ||
|
2020-11-19 18:10:40 +03:00
|
|
|
!this.authStepIsUsed('m.login.msisdn')
|
2019-10-28 17:13:24 +03:00
|
|
|
) {
|
2019-08-28 17:34:50 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-08-28 17:34:50 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private renderEmail() {
|
|
|
|
if (!this.showEmail()) {
|
2019-04-19 00:53:46 +03:00
|
|
|
return null;
|
|
|
|
}
|
2020-11-19 18:10:40 +03:00
|
|
|
const emailPlaceholder = this.authStepIsRequired('m.login.email.identity') ?
|
2019-04-19 00:53:46 +03:00
|
|
|
_t("Email") :
|
|
|
|
_t("Email (optional)");
|
|
|
|
return <Field
|
2020-11-19 18:10:40 +03:00
|
|
|
ref={field => this[RegistrationField.Email] = field}
|
2019-04-19 00:53:46 +03:00
|
|
|
type="text"
|
|
|
|
label={emailPlaceholder}
|
|
|
|
value={this.state.email}
|
|
|
|
onChange={this.onEmailChange}
|
|
|
|
onValidate={this.onEmailValidate}
|
2020-10-29 18:53:14 +03:00
|
|
|
onFocus={() => CountlyAnalytics.instance.track("onboarding_registration_email_focus")}
|
|
|
|
onBlur={() => CountlyAnalytics.instance.track("onboarding_registration_email_blur")}
|
2019-04-19 00:53:46 +03:00
|
|
|
/>;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-04-19 00:53:46 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
private renderPassword() {
|
2020-05-14 22:19:15 +03:00
|
|
|
return <PassphraseField
|
2019-04-19 01:29:05 +03:00
|
|
|
id="mx_RegistrationForm_password"
|
2020-11-19 18:10:40 +03:00
|
|
|
fieldRef={field => this[RegistrationField.Password] = field}
|
2020-05-14 22:19:15 +03:00
|
|
|
minScore={PASSWORD_MIN_SCORE}
|
2019-04-19 01:29:05 +03:00
|
|
|
value={this.state.password}
|
|
|
|
onChange={this.onPasswordChange}
|
|
|
|
onValidate={this.onPasswordValidate}
|
2020-10-29 18:53:14 +03:00
|
|
|
onFocus={() => CountlyAnalytics.instance.track("onboarding_registration_password_focus")}
|
|
|
|
onBlur={() => CountlyAnalytics.instance.track("onboarding_registration_password_blur")}
|
2019-04-19 01:29:05 +03:00
|
|
|
/>;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-04-19 01:29:05 +03:00
|
|
|
|
|
|
|
renderPasswordConfirm() {
|
|
|
|
return <Field
|
|
|
|
id="mx_RegistrationForm_passwordConfirm"
|
2020-11-19 18:10:40 +03:00
|
|
|
ref={field => this[RegistrationField.PasswordConfirm] = field}
|
2019-04-19 01:29:05 +03:00
|
|
|
type="password"
|
2020-01-29 16:38:50 +03:00
|
|
|
autoComplete="new-password"
|
2020-11-11 16:45:50 +03:00
|
|
|
label={_t("Confirm password")}
|
2019-04-19 01:29:05 +03:00
|
|
|
value={this.state.passwordConfirm}
|
|
|
|
onChange={this.onPasswordConfirmChange}
|
|
|
|
onValidate={this.onPasswordConfirmValidate}
|
2020-10-29 18:53:14 +03:00
|
|
|
onFocus={() => CountlyAnalytics.instance.track("onboarding_registration_passwordConfirm_focus")}
|
|
|
|
onBlur={() => CountlyAnalytics.instance.track("onboarding_registration_passwordConfirm_blur")}
|
2019-04-19 01:29:05 +03:00
|
|
|
/>;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-04-19 01:29:05 +03:00
|
|
|
|
2019-04-19 01:05:55 +03:00
|
|
|
renderPhoneNumber() {
|
2020-11-19 18:10:40 +03:00
|
|
|
if (!this.showPhoneNumber()) {
|
2019-04-19 01:05:55 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const CountryDropdown = sdk.getComponent('views.auth.CountryDropdown');
|
2020-11-19 18:10:40 +03:00
|
|
|
const phoneLabel = this.authStepIsRequired('m.login.msisdn') ?
|
2019-04-19 01:05:55 +03:00
|
|
|
_t("Phone") :
|
|
|
|
_t("Phone (optional)");
|
|
|
|
const phoneCountry = <CountryDropdown
|
|
|
|
value={this.state.phoneCountry}
|
|
|
|
isSmall={true}
|
|
|
|
showPrefix={true}
|
|
|
|
onOptionChange={this.onPhoneCountryChange}
|
|
|
|
/>;
|
|
|
|
return <Field
|
2020-11-19 18:10:40 +03:00
|
|
|
ref={field => this[RegistrationField.PhoneNumber] = field}
|
2019-04-19 01:05:55 +03:00
|
|
|
type="text"
|
|
|
|
label={phoneLabel}
|
|
|
|
value={this.state.phoneNumber}
|
2020-05-28 23:09:42 +03:00
|
|
|
prefixComponent={phoneCountry}
|
2019-04-19 01:05:55 +03:00
|
|
|
onChange={this.onPhoneNumberChange}
|
|
|
|
onValidate={this.onPhoneNumberValidate}
|
|
|
|
/>;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-04-19 01:05:55 +03:00
|
|
|
|
2019-04-16 18:52:31 +03:00
|
|
|
renderUsername() {
|
|
|
|
return <Field
|
|
|
|
id="mx_RegistrationForm_username"
|
2020-11-19 18:10:40 +03:00
|
|
|
ref={field => this[RegistrationField.Username] = field}
|
2019-04-16 18:52:31 +03:00
|
|
|
type="text"
|
|
|
|
autoFocus={true}
|
|
|
|
label={_t("Username")}
|
2020-11-23 13:23:28 +03:00
|
|
|
placeholder={_t("Username").toLocaleLowerCase()}
|
2019-04-16 18:52:31 +03:00
|
|
|
value={this.state.username}
|
|
|
|
onChange={this.onUsernameChange}
|
2019-04-17 13:39:11 +03:00
|
|
|
onValidate={this.onUsernameValidate}
|
2020-10-29 18:53:14 +03:00
|
|
|
onFocus={() => CountlyAnalytics.instance.track("onboarding_registration_username_focus")}
|
|
|
|
onBlur={() => CountlyAnalytics.instance.track("onboarding_registration_username_blur")}
|
2019-04-16 18:52:31 +03:00
|
|
|
/>;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-04-16 18:52:31 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
render() {
|
2017-02-24 14:41:23 +03:00
|
|
|
const registerButton = (
|
2019-06-05 08:41:59 +03:00
|
|
|
<input className="mx_Login_submit" type="submit" value={_t("Register")} disabled={!this.props.canSubmit} />
|
2017-02-24 14:41:23 +03:00
|
|
|
);
|
|
|
|
|
2019-08-28 17:34:50 +03:00
|
|
|
let emailHelperText = null;
|
2020-11-19 18:10:40 +03:00
|
|
|
if (this.showEmail()) {
|
|
|
|
if (this.showPhoneNumber()) {
|
2019-08-28 17:34:50 +03:00
|
|
|
emailHelperText = <div>
|
|
|
|
{_t(
|
|
|
|
"Set an email for account recovery. " +
|
|
|
|
"Use email or phone to optionally be discoverable by existing contacts.",
|
|
|
|
)}
|
|
|
|
</div>;
|
|
|
|
} else {
|
|
|
|
emailHelperText = <div>
|
|
|
|
{_t(
|
|
|
|
"Set an email for account recovery. " +
|
|
|
|
"Use email to optionally be discoverable by existing contacts.",
|
|
|
|
)}
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:15:56 +03:00
|
|
|
const haveIs = Boolean(this.props.serverConfig.isUrl);
|
2019-10-28 17:13:24 +03:00
|
|
|
let noIsText = null;
|
|
|
|
if (this.props.serverRequiresIdServer && !haveIs) {
|
|
|
|
noIsText = <div>
|
|
|
|
{_t(
|
2019-10-28 17:23:11 +03:00
|
|
|
"No identity server is configured so you cannot add an email address in order to " +
|
|
|
|
"reset your password in the future.",
|
2019-10-28 17:13:24 +03:00
|
|
|
)}
|
|
|
|
</div>;
|
|
|
|
}
|
2019-08-07 13:15:56 +03:00
|
|
|
|
2015-11-30 21:11:04 +03:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<form onSubmit={this.onSubmit}>
|
2019-01-30 21:46:40 +03:00
|
|
|
<div className="mx_AuthBody_fieldRow">
|
2019-04-16 18:52:31 +03:00
|
|
|
{this.renderUsername()}
|
2019-01-30 00:52:12 +03:00
|
|
|
</div>
|
2019-01-30 21:46:40 +03:00
|
|
|
<div className="mx_AuthBody_fieldRow">
|
2019-04-19 01:29:05 +03:00
|
|
|
{this.renderPassword()}
|
|
|
|
{this.renderPasswordConfirm()}
|
2019-01-30 00:52:12 +03:00
|
|
|
</div>
|
2019-01-30 21:46:40 +03:00
|
|
|
<div className="mx_AuthBody_fieldRow">
|
2019-04-19 00:53:46 +03:00
|
|
|
{this.renderEmail()}
|
2019-04-19 01:05:55 +03:00
|
|
|
{this.renderPhoneNumber()}
|
2019-01-30 00:52:12 +03:00
|
|
|
</div>
|
2019-08-07 13:15:56 +03:00
|
|
|
{ emailHelperText }
|
|
|
|
{ noIsText }
|
2017-10-11 19:56:17 +03:00
|
|
|
{ registerButton }
|
2015-11-30 21:11:04 +03:00
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|