Make registration work with server configs

The general idea is that we throw the object around between components so they can pull off the details they care about.
pull/21833/head
Travis Ralston 2019-05-02 23:04:06 -06:00
parent 6b45e60314
commit 00ebb5e1fd
1 changed files with 45 additions and 58 deletions

View File

@ -17,16 +17,15 @@ limitations under the License.
*/ */
import Matrix from 'matrix-js-sdk'; import Matrix from 'matrix-js-sdk';
import Promise from 'bluebird'; import Promise from 'bluebird';
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import sdk from '../../../index'; import sdk from '../../../index';
import { _t, _td } from '../../../languageHandler'; import { _t, _td } from '../../../languageHandler';
import SdkConfig from '../../../SdkConfig'; import SdkConfig from '../../../SdkConfig';
import { messageForResourceLimitError } from '../../../utils/ErrorUtils'; import { messageForResourceLimitError } from '../../../utils/ErrorUtils';
import * as ServerType from '../../views/auth/ServerTypeSelector'; import * as ServerType from '../../views/auth/ServerTypeSelector';
import {ValidatedServerConfig} from "../../../utils/AutoDiscoveryUtils";
// Phases // Phases
// Show controls to configure server details // Show controls to configure server details
@ -46,18 +45,7 @@ module.exports = React.createClass({
sessionId: PropTypes.string, sessionId: PropTypes.string,
makeRegistrationUrl: PropTypes.func.isRequired, makeRegistrationUrl: PropTypes.func.isRequired,
idSid: PropTypes.string, idSid: PropTypes.string,
// The default server name to use when the user hasn't specified serverConfig: PropTypes.instanceOf(ValidatedServerConfig).isRequired,
// one. If set, `defaultHsUrl` and `defaultHsUrl` were derived for this
// via `.well-known` discovery. The server name is used instead of the
// HS URL when talking about "your account".
defaultServerName: PropTypes.string,
// An error passed along from higher up explaining that something
// went wrong when finding the defaultHsUrl.
defaultServerDiscoveryError: PropTypes.string,
customHsUrl: PropTypes.string,
customIsUrl: PropTypes.string,
defaultHsUrl: PropTypes.string,
defaultIsUrl: PropTypes.string,
brand: PropTypes.string, brand: PropTypes.string,
email: PropTypes.string, email: PropTypes.string,
// registration shouldn't know or care how login is done. // registration shouldn't know or care how login is done.
@ -66,7 +54,7 @@ module.exports = React.createClass({
}, },
getInitialState: function() { getInitialState: function() {
const serverType = ServerType.getTypeFromHsUrl(this.props.customHsUrl); const serverType = ServerType.getTypeFromServerConfig(this.props.serverConfig);
return { return {
busy: false, busy: false,
@ -87,8 +75,6 @@ module.exports = React.createClass({
// straight back into UI auth // straight back into UI auth
doingUIAuth: Boolean(this.props.sessionId), doingUIAuth: Boolean(this.props.sessionId),
serverType, serverType,
hsUrl: this.props.customHsUrl,
isUrl: this.props.customIsUrl,
// Phase of the overall registration dialog. // Phase of the overall registration dialog.
phase: PHASE_REGISTRATION, phase: PHASE_REGISTRATION,
flows: null, flows: null,
@ -100,18 +86,22 @@ module.exports = React.createClass({
this._replaceClient(); this._replaceClient();
}, },
onServerConfigChange: function(config) { componentWillReceiveProps(newProps) {
const newState = {}; if (newProps.serverConfig.hsUrl === this.props.serverConfig.hsUrl &&
if (config.hsUrl !== undefined) { newProps.serverConfig.isUrl === this.props.serverConfig.isUrl) return;
newState.hsUrl = config.hsUrl;
} this._replaceClient(newProps.serverConfig);
if (config.isUrl !== undefined) {
newState.isUrl = config.isUrl; // Handle cases where the user enters "https://matrix.org" for their server
} // from the advanced option - we should default to FREE at that point.
this.props.onServerConfigChange(config); const serverType = ServerType.getTypeFromServerConfig(newProps.serverConfig);
this.setState(newState, () => { if (serverType !== this.state.serverType) {
this._replaceClient(); // Reset the phase to default phase for the server type.
this.setState({
serverType,
phase: this.getDefaultPhaseForServerType(serverType),
}); });
}
}, },
getDefaultPhaseForServerType(type) { getDefaultPhaseForServerType(type) {
@ -136,19 +126,17 @@ module.exports = React.createClass({
// the new type. // the new type.
switch (type) { switch (type) {
case ServerType.FREE: { case ServerType.FREE: {
const { hsUrl, isUrl } = ServerType.TYPES.FREE; const { serverConfig } = ServerType.TYPES.FREE;
this.onServerConfigChange({ this.props.onServerConfigChange(serverConfig);
hsUrl,
isUrl,
});
break; break;
} }
case ServerType.PREMIUM: case ServerType.PREMIUM:
// We can accept whatever server config was the default here as this essentially
// acts as a slightly different "custom server"/ADVANCED option.
break;
case ServerType.ADVANCED: case ServerType.ADVANCED:
this.onServerConfigChange({ // Use the default config from the config
hsUrl: this.props.defaultHsUrl, this.props.onServerConfigChange(SdkConfig.get()["validated_server_config"]);
isUrl: this.props.defaultIsUrl,
});
break; break;
} }
@ -158,13 +146,15 @@ module.exports = React.createClass({
}); });
}, },
_replaceClient: async function() { _replaceClient: async function(serverConfig) {
this.setState({ this.setState({
errorText: null, errorText: null,
}); });
if (!serverConfig) serverConfig = this.props.serverConfig;
const {hsUrl, isUrl} = serverConfig;
this._matrixClient = Matrix.createClient({ this._matrixClient = Matrix.createClient({
baseUrl: this.state.hsUrl, baseUrl: hsUrl,
idBaseUrl: this.state.isUrl, idBaseUrl: isUrl,
}); });
try { try {
await this._makeRegisterRequest({}); await this._makeRegisterRequest({});
@ -189,12 +179,6 @@ module.exports = React.createClass({
}, },
onFormSubmit: function(formVals) { onFormSubmit: function(formVals) {
// Don't allow the user to register if there's a discovery error
// Without this, the user could end up registering on the wrong homeserver.
if (this.props.defaultServerDiscoveryError) {
this.setState({errorText: this.props.defaultServerDiscoveryError});
return;
}
this.setState({ this.setState({
errorText: "", errorText: "",
busy: true, busy: true,
@ -207,7 +191,7 @@ module.exports = React.createClass({
if (!success) { if (!success) {
let msg = response.message || response.toString(); let msg = response.message || response.toString();
// can we give a better error message? // can we give a better error message?
if (response.errcode == 'M_RESOURCE_LIMIT_EXCEEDED') { if (response.errcode === 'M_RESOURCE_LIMIT_EXCEEDED') {
const errorTop = messageForResourceLimitError( const errorTop = messageForResourceLimitError(
response.data.limit_type, response.data.limit_type,
response.data.admin_contact, { response.data.admin_contact, {
@ -302,8 +286,13 @@ module.exports = React.createClass({
}); });
}, },
onServerDetailsNextPhaseClick(ev) { async onServerDetailsNextPhaseClick(ev) {
ev.stopPropagation(); ev.stopPropagation();
// TODO: TravisR - Capture the user's input somehow else
if (this._serverConfigRef) {
// Just to make sure the user's input gets captured
await this._serverConfigRef.validateServer();
}
this.setState({ this.setState({
phase: PHASE_REGISTRATION, phase: PHASE_REGISTRATION,
}); });
@ -371,20 +360,17 @@ module.exports = React.createClass({
break; break;
case ServerType.PREMIUM: case ServerType.PREMIUM:
serverDetails = <ModularServerConfig serverDetails = <ModularServerConfig
customHsUrl={this.state.discoveredHsUrl || this.props.customHsUrl} ref={r => this._serverConfigRef = r}
defaultHsUrl={this.props.defaultHsUrl} serverConfig={this.props.serverConfig}
defaultIsUrl={this.props.defaultIsUrl} onServerConfigChange={this.props.onServerConfigChange}
onServerConfigChange={this.onServerConfigChange}
delayTimeMs={250} delayTimeMs={250}
/>; />;
break; break;
case ServerType.ADVANCED: case ServerType.ADVANCED:
serverDetails = <ServerConfig serverDetails = <ServerConfig
customHsUrl={this.state.discoveredHsUrl || this.props.customHsUrl} ref={r => this._serverConfigRef = r}
customIsUrl={this.state.discoveredIsUrl || this.props.customIsUrl} serverConfig={this.props.serverConfig}
defaultHsUrl={this.props.defaultHsUrl} onServerConfigChange={this.props.onServerConfigChange}
defaultIsUrl={this.props.defaultIsUrl}
onServerConfigChange={this.onServerConfigChange}
delayTimeMs={250} delayTimeMs={250}
/>; />;
break; break;
@ -392,6 +378,7 @@ module.exports = React.createClass({
let nextButton = null; let nextButton = null;
if (PHASES_ENABLED) { if (PHASES_ENABLED) {
// TODO: TravisR - Pull out server discovery from ServerConfig to disable the next button?
nextButton = <AccessibleButton className="mx_Login_submit" nextButton = <AccessibleButton className="mx_Login_submit"
onClick={this.onServerDetailsNextPhaseClick} onClick={this.onServerDetailsNextPhaseClick}
> >
@ -466,7 +453,7 @@ module.exports = React.createClass({
const AuthPage = sdk.getComponent('auth.AuthPage'); const AuthPage = sdk.getComponent('auth.AuthPage');
let errorText; let errorText;
const err = this.state.errorText || this.props.defaultServerDiscoveryError; const err = this.state.errorText;
if (err) { if (err) {
errorText = <div className="mx_Login_error">{ err }</div>; errorText = <div className="mx_Login_error">{ err }</div>;
} }