From 3cf9f786aa1b6ca1f6dd3cb088f5c0d27fe57a99 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 19 Nov 2015 15:44:17 +0000 Subject: [PATCH] Instantiate Signup.Register in Registration component This has to be done rather than in MatrixChat because the render() calls will create new instances otherwise. Pass in all the strings the logic class requires to the Registration wire component. This isn't the "best" solution because unloading/reloading the Registration component will lose registration state which should be persisted. Ideally we'd DI from the top to ensure this can't happen (as opposed to relying on module globals...) --- src/components/login/CaptchaForm.js | 1 - src/components/login/Registration.js | 52 +++++++++++++++++----- src/skins/vector/views/pages/MatrixChat.js | 26 +++-------- 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/src/components/login/CaptchaForm.js b/src/components/login/CaptchaForm.js index 81e5136c..f82ace8e 100644 --- a/src/components/login/CaptchaForm.js +++ b/src/components/login/CaptchaForm.js @@ -40,7 +40,6 @@ module.exports = React.createClass({ componentDidMount: function() { // Just putting a script tag into the returned jsx doesn't work, annoyingly, // so we do this instead. - console.log("CDM"); var self = this; if (this.refs.recaptchaContainer) { console.log("Loading recaptcha script..."); diff --git a/src/components/login/Registration.js b/src/components/login/Registration.js index 627f9b27..99b58ad1 100644 --- a/src/components/login/Registration.js +++ b/src/components/login/Registration.js @@ -24,6 +24,7 @@ var dis = require('matrix-react-sdk/lib/dispatcher'); var ServerConfig = require("./ServerConfig"); var RegistrationForm = require("./RegistrationForm"); var CaptchaForm = require("./CaptchaForm"); +var Signup = require("matrix-react-sdk/lib/Signup"); var MIN_PASSWORD_LENGTH = 6; module.exports = React.createClass({ @@ -31,7 +32,12 @@ module.exports = React.createClass({ propTypes: { onLoggedIn: React.PropTypes.func.isRequired, - registerLogic: React.PropTypes.any.isRequired, + clientSecret: React.PropTypes.string, + sessionId: React.PropTypes.string, + registrationUrl: React.PropTypes.string, + idSid: React.PropTypes.string, + hsUrl: React.PropTypes.string, + isUrl: React.PropTypes.string, // registration shouldn't know or care how login is done. onLoginClick: React.PropTypes.func.isRequired }, @@ -40,25 +46,43 @@ module.exports = React.createClass({ return { busy: false, errorText: null, - enteredHomeserverUrl: this.props.registerLogic.getHomeserverUrl(), - enteredIdentityServerUrl: this.props.registerLogic.getIdentityServerUrl() + enteredHomeserverUrl: this.props.hsUrl, + enteredIdentityServerUrl: this.props.isUrl }; }, componentWillMount: function() { this.dispatcherRef = dis.register(this.onAction); + // attach this to the instance rather than this.state since it isn't UI + this.registerLogic = new Signup.Register( + this.props.hsUrl, this.props.isUrl + ); + this.registerLogic.setClientSecret(this.props.clientSecret); + this.registerLogic.setSessionId(this.props.sessionId); + this.registerLogic.setRegistrationUrl(this.props.registrationUrl); + this.registerLogic.setIdSid(this.props.idSid); + this.registerLogic.recheckState(); }, componentWillUnmount: function() { dis.unregister(this.dispatcherRef); }, + componentDidMount: function() { + // may have already done an HTTP hit (e.g. redirect from an email) so + // check for any pending response + var promise = this.registerLogic.getPromise(); + if (promise) { + this.onProcessingRegistration(promise); + } + }, + onHsUrlChanged: function(newHsUrl) { - this.props.registerLogic.setHomeserverUrl(newHsUrl); + this.registerLogic.setHomeserverUrl(newHsUrl); }, onIsUrlChanged: function(newIsUrl) { - this.props.registerLogic.setIdentityServerUrl(newIsUrl); + this.registerLogic.setIdentityServerUrl(newIsUrl); }, onAction: function(payload) { @@ -70,14 +94,20 @@ module.exports = React.createClass({ onFormSubmit: function(formVals) { var self = this; - this.props.registerLogic.register(formVals).done(function(response) { + this.onProcessingRegistration(this.registerLogic.register(formVals)); + }, + + // Promise is resolved when the registration process is FULLY COMPLETE + onProcessingRegistration: function(promise) { + var self = this; + promise.done(function(response) { if (!response || !response.access_token) { console.warn( "FIXME: Register fulfilled without a final response, " + "did you break the promise chain?" ); // no matter, we'll grab it direct - response = self.props.registerLogic.getCredentials(); + response = self.registerLogic.getCredentials(); } if (!response || !response.user_id || !response.access_token) { console.error("Final response is missing keys."); @@ -88,8 +118,8 @@ module.exports = React.createClass({ } self.props.onLoggedIn({ userId: response.user_id, - homeserverUrl: self.props.registerLogic.getHomeserverUrl(), - identityServerUrl: self.props.registerLogic.getIdentityServerUrl(), + homeserverUrl: self.registerLogic.getHomeserverUrl(), + identityServerUrl: self.registerLogic.getIdentityServerUrl(), accessToken: response.access_token }); }, function(err) { @@ -125,7 +155,7 @@ module.exports = React.createClass({ }, onCaptchaLoaded: function() { - this.props.registerLogic.tellStage("m.login.recaptcha", "loaded"); + this.registerLogic.tellStage("m.login.recaptcha", "loaded"); }, // TODO: @@ -148,7 +178,7 @@ module.exports = React.createClass({ }, _getRegisterContentJsx: function() { - var currStep = this.props.registerLogic.getStep(); + var currStep = this.registerLogic.getStep(); var registerStep; switch (currStep) { case "Register.COMPLETE": diff --git a/src/skins/vector/views/pages/MatrixChat.js b/src/skins/vector/views/pages/MatrixChat.js index ef023157..3d585651 100644 --- a/src/skins/vector/views/pages/MatrixChat.js +++ b/src/skins/vector/views/pages/MatrixChat.js @@ -27,7 +27,6 @@ var Matrix = require("matrix-js-sdk"); var ContextualMenu = require("../../../../ContextualMenu") var Login = require("../../../../components/login/Login"); var Registration = require("../../../../components/login/Registration"); -var Signup = require("matrix-react-sdk/lib/Signup"); var config = require("../../../../../config.json"); module.exports = React.createClass({ @@ -178,27 +177,16 @@ module.exports = React.createClass({ ); } else if (this.state.screen == 'register') { - /* - return ( - - ); */ - var registerLogic = new Signup.Register( - config.default_hs_url, config.default_is_url - ); - registerLogic.setClientSecret(this.state.register_client_secret); - registerLogic.setSessionId(this.state.register_session_id); - registerLogic.setRegistrationUrl(this.props.registrationUrl); - registerLogic.setIdSid(this.state.register_id_sid); - registerLogic.recheckState(); return ( + onLoginClick={this.onLoginClick} /> ); } else { return (