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...)
pull/399/head
Kegan Dougal 2015-11-19 15:44:17 +00:00
parent 2d481a6302
commit 3cf9f786aa
3 changed files with 48 additions and 31 deletions

View File

@ -40,7 +40,6 @@ module.exports = React.createClass({
componentDidMount: function() { componentDidMount: function() {
// Just putting a script tag into the returned jsx doesn't work, annoyingly, // Just putting a script tag into the returned jsx doesn't work, annoyingly,
// so we do this instead. // so we do this instead.
console.log("CDM");
var self = this; var self = this;
if (this.refs.recaptchaContainer) { if (this.refs.recaptchaContainer) {
console.log("Loading recaptcha script..."); console.log("Loading recaptcha script...");

View File

@ -24,6 +24,7 @@ var dis = require('matrix-react-sdk/lib/dispatcher');
var ServerConfig = require("./ServerConfig"); var ServerConfig = require("./ServerConfig");
var RegistrationForm = require("./RegistrationForm"); var RegistrationForm = require("./RegistrationForm");
var CaptchaForm = require("./CaptchaForm"); var CaptchaForm = require("./CaptchaForm");
var Signup = require("matrix-react-sdk/lib/Signup");
var MIN_PASSWORD_LENGTH = 6; var MIN_PASSWORD_LENGTH = 6;
module.exports = React.createClass({ module.exports = React.createClass({
@ -31,7 +32,12 @@ module.exports = React.createClass({
propTypes: { propTypes: {
onLoggedIn: React.PropTypes.func.isRequired, 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. // registration shouldn't know or care how login is done.
onLoginClick: React.PropTypes.func.isRequired onLoginClick: React.PropTypes.func.isRequired
}, },
@ -40,25 +46,43 @@ module.exports = React.createClass({
return { return {
busy: false, busy: false,
errorText: null, errorText: null,
enteredHomeserverUrl: this.props.registerLogic.getHomeserverUrl(), enteredHomeserverUrl: this.props.hsUrl,
enteredIdentityServerUrl: this.props.registerLogic.getIdentityServerUrl() enteredIdentityServerUrl: this.props.isUrl
}; };
}, },
componentWillMount: function() { componentWillMount: function() {
this.dispatcherRef = dis.register(this.onAction); 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() { componentWillUnmount: function() {
dis.unregister(this.dispatcherRef); 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) { onHsUrlChanged: function(newHsUrl) {
this.props.registerLogic.setHomeserverUrl(newHsUrl); this.registerLogic.setHomeserverUrl(newHsUrl);
}, },
onIsUrlChanged: function(newIsUrl) { onIsUrlChanged: function(newIsUrl) {
this.props.registerLogic.setIdentityServerUrl(newIsUrl); this.registerLogic.setIdentityServerUrl(newIsUrl);
}, },
onAction: function(payload) { onAction: function(payload) {
@ -70,14 +94,20 @@ module.exports = React.createClass({
onFormSubmit: function(formVals) { onFormSubmit: function(formVals) {
var self = this; 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) { if (!response || !response.access_token) {
console.warn( console.warn(
"FIXME: Register fulfilled without a final response, " + "FIXME: Register fulfilled without a final response, " +
"did you break the promise chain?" "did you break the promise chain?"
); );
// no matter, we'll grab it direct // no matter, we'll grab it direct
response = self.props.registerLogic.getCredentials(); response = self.registerLogic.getCredentials();
} }
if (!response || !response.user_id || !response.access_token) { if (!response || !response.user_id || !response.access_token) {
console.error("Final response is missing keys."); console.error("Final response is missing keys.");
@ -88,8 +118,8 @@ module.exports = React.createClass({
} }
self.props.onLoggedIn({ self.props.onLoggedIn({
userId: response.user_id, userId: response.user_id,
homeserverUrl: self.props.registerLogic.getHomeserverUrl(), homeserverUrl: self.registerLogic.getHomeserverUrl(),
identityServerUrl: self.props.registerLogic.getIdentityServerUrl(), identityServerUrl: self.registerLogic.getIdentityServerUrl(),
accessToken: response.access_token accessToken: response.access_token
}); });
}, function(err) { }, function(err) {
@ -125,7 +155,7 @@ module.exports = React.createClass({
}, },
onCaptchaLoaded: function() { onCaptchaLoaded: function() {
this.props.registerLogic.tellStage("m.login.recaptcha", "loaded"); this.registerLogic.tellStage("m.login.recaptcha", "loaded");
}, },
// TODO: // TODO:
@ -148,7 +178,7 @@ module.exports = React.createClass({
}, },
_getRegisterContentJsx: function() { _getRegisterContentJsx: function() {
var currStep = this.props.registerLogic.getStep(); var currStep = this.registerLogic.getStep();
var registerStep; var registerStep;
switch (currStep) { switch (currStep) {
case "Register.COMPLETE": case "Register.COMPLETE":

View File

@ -27,7 +27,6 @@ var Matrix = require("matrix-js-sdk");
var ContextualMenu = require("../../../../ContextualMenu") var ContextualMenu = require("../../../../ContextualMenu")
var Login = require("../../../../components/login/Login"); var Login = require("../../../../components/login/Login");
var Registration = require("../../../../components/login/Registration"); var Registration = require("../../../../components/login/Registration");
var Signup = require("matrix-react-sdk/lib/Signup");
var config = require("../../../../../config.json"); var config = require("../../../../../config.json");
module.exports = React.createClass({ module.exports = React.createClass({
@ -178,27 +177,16 @@ module.exports = React.createClass({
</div> </div>
); );
} else if (this.state.screen == 'register') { } else if (this.state.screen == 'register') {
/*
return (
<Register onLoggedIn={this.onLoggedIn} clientSecret={this.state.register_client_secret}
sessionId={this.state.register_session_id} idSid={this.state.register_id_sid}
hsUrl={this.state.register_hs_url} isUrl={this.state.register_is_url}
registrationUrl={this.props.registrationUrl}
/>
); */
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 ( return (
<Registration <Registration
clientSecret={this.state.register_client_secret}
sessionId={this.state.register_session_id}
idSid={this.state.register_id_sid}
hsUrl={config.default_hs_url}
isUrl={config.default_is_url}
registrationUrl={this.props.registrationUrl}
onLoggedIn={this.onLoggedIn} onLoggedIn={this.onLoggedIn}
onLoginClick={this.onLoginClick} onLoginClick={this.onLoginClick} />
registerLogic={registerLogic} />
); );
} else { } else {
return ( return (