/* Copyright 2015, 2016 OpenMarket Ltd Copyright 2017 Vector Creations Ltd Copyright 2018, 2019 New Vector Ltd 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. */ import Matrix from 'matrix-js-sdk'; import Promise from 'bluebird'; import React from 'react'; import PropTypes from 'prop-types'; import sdk from '../../../index'; import { _t, _td } from '../../../languageHandler'; import SdkConfig from '../../../SdkConfig'; import { messageForResourceLimitError } from '../../../utils/ErrorUtils'; import * as ServerType from '../../views/auth/ServerTypeSelector'; // Phases // Show controls to configure server details const PHASE_SERVER_DETAILS = 0; // Show the appropriate registration flow(s) for the server const PHASE_REGISTRATION = 1; // Enable phases for registration const PHASES_ENABLED = true; module.exports = React.createClass({ displayName: 'Registration', propTypes: { onLoggedIn: PropTypes.func.isRequired, clientSecret: PropTypes.string, sessionId: PropTypes.string, makeRegistrationUrl: PropTypes.func.isRequired, idSid: PropTypes.string, // The default server name to use when the user hasn't specified // 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, email: PropTypes.string, // registration shouldn't know or care how login is done. onLoginClick: PropTypes.func.isRequired, onServerConfigChange: PropTypes.func.isRequired, }, getInitialState: function() { const serverType = ServerType.getTypeFromHsUrl(this.props.customHsUrl); return { busy: false, errorText: null, // We remember the values entered by the user because // the registration form will be unmounted during the // course of registration, but if there's an error we // want to bring back the registration form with the // values the user entered still in it. We can keep // them in this component's state since this component // persist for the duration of the registration process. formVals: { email: this.props.email, }, // true if we're waiting for the user to complete // user-interactive auth // If we've been given a session ID, we're resuming // straight back into UI auth doingUIAuth: Boolean(this.props.sessionId), serverType, hsUrl: this.props.customHsUrl, isUrl: this.props.customIsUrl, // Phase of the overall registration dialog. phase: PHASE_REGISTRATION, flows: null, }; }, componentWillMount: function() { this._unmounted = false; this._replaceClient(); }, onServerConfigChange: function(config) { const newState = {}; if (config.hsUrl !== undefined) { newState.hsUrl = config.hsUrl; } if (config.isUrl !== undefined) { newState.isUrl = config.isUrl; } this.props.onServerConfigChange(config); this.setState(newState, () => { this._replaceClient(); }); }, getDefaultPhaseForServerType(type) { switch (type) { case ServerType.FREE: { // Move directly to the registration phase since the server // details are fixed. return PHASE_REGISTRATION; } case ServerType.PREMIUM: case ServerType.ADVANCED: return PHASE_SERVER_DETAILS; } }, onServerTypeChange(type) { this.setState({ serverType: type, }); // When changing server types, set the HS / IS URLs to reasonable defaults for the // the new type. switch (type) { case ServerType.FREE: { const { hsUrl, isUrl } = ServerType.TYPES.FREE; this.onServerConfigChange({ hsUrl, isUrl, }); break; } case ServerType.PREMIUM: case ServerType.ADVANCED: this.onServerConfigChange({ hsUrl: this.props.defaultHsUrl, isUrl: this.props.defaultIsUrl, }); break; } // Reset the phase to default phase for the server type. this.setState({ phase: this.getDefaultPhaseForServerType(type), }); }, _replaceClient: async function() { this.setState({ errorText: null, }); this._matrixClient = Matrix.createClient({ baseUrl: this.state.hsUrl, idBaseUrl: this.state.isUrl, }); try { await this._makeRegisterRequest({}); // This should never succeed since we specified an empty // auth object. console.log("Expecting 401 from register request but got success!"); } catch (e) { if (e.httpStatus === 401) { this.setState({ flows: e.data.flows, }); } else if (e.httpStatus === 403 && e.errcode === "M_UNKNOWN") { this.setState({ errorText: _t("Registration has been disabled on this homeserver."), }); } else { this.setState({ errorText: _t("Unable to query for supported registration methods."), }); } } }, 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({ errorText: "", busy: true, formVals: formVals, doingUIAuth: true, }); }, _onUIAuthFinished: async function(success, response, extra) { if (!success) { let msg = response.message || response.toString(); // can we give a better error message? if (response.errcode == 'M_RESOURCE_LIMIT_EXCEEDED') { const errorTop = messageForResourceLimitError( response.data.limit_type, response.data.admin_contact, { 'monthly_active_user': _td( "This homeserver has hit its Monthly Active User limit.", ), '': _td( "This homeserver has exceeded one of its resource limits.", ), }); const errorDetail = messageForResourceLimitError( response.data.limit_type, response.data.admin_contact, { '': _td( "Please contact your service administrator to continue using this service.", ), }); msg =
{errorTop}
{errorDetail}