From eaafc11064b6daa976d635706ad68e8bf120db9d Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 19 Nov 2015 13:44:11 +0000 Subject: [PATCH] Factor out Captcha UI --- src/components/login/CaptchaForm.js | 68 ++++++++++++++++++++++ src/components/login/Registration.js | 37 +++++------- src/skins/vector/views/pages/MatrixChat.js | 4 +- 3 files changed, 85 insertions(+), 24 deletions(-) create mode 100644 src/components/login/CaptchaForm.js diff --git a/src/components/login/CaptchaForm.js b/src/components/login/CaptchaForm.js new file mode 100644 index 0000000000..81e5136cef --- /dev/null +++ b/src/components/login/CaptchaForm.js @@ -0,0 +1,68 @@ +/* +Copyright 2015 OpenMarket 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. +*/ + +'use strict'; + +var React = require('react'); +var sdk = require('matrix-react-sdk') + +/** + * A pure UI component which displays a captcha form. + */ +module.exports = React.createClass({ + displayName: 'CaptchaForm', + + propTypes: { + onCaptchaLoaded: React.PropTypes.func.isRequired + }, + + getDefaultProps: function() { + return { + onCaptchaLoaded: function() { + console.error("Unhandled onCaptchaLoaded"); + } + }; + }, + + 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..."); + var scriptTag = document.createElement('script'); + window.mx_on_recaptcha_loaded = function() { + console.log("Loaded recaptcha script."); + self.props.onCaptchaLoaded(); + }; + scriptTag.setAttribute( + 'src', global.location.protocol+"//www.google.com/recaptcha/api.js?onload=mx_on_recaptcha_loaded&render=explicit" + ); + this.refs.recaptchaContainer.appendChild(scriptTag); + } + }, + + render: function() { + // FIXME: Tight coupling with the div id and SignupStages.js + return ( +
+ This Home Server would like to make sure you are not a robot +
+
+ ); + } +}); \ No newline at end of file diff --git a/src/components/login/Registration.js b/src/components/login/Registration.js index ea121ba022..38cc51b7b4 100644 --- a/src/components/login/Registration.js +++ b/src/components/login/Registration.js @@ -23,6 +23,7 @@ var MatrixClientPeg = require('matrix-react-sdk/lib/MatrixClientPeg'); var dis = require('matrix-react-sdk/lib/dispatcher'); var ServerConfig = require("./ServerConfig"); var RegistrationForm = require("./RegistrationForm"); +var CaptchaForm = require("./CaptchaForm"); var MIN_PASSWORD_LENGTH = 6; module.exports = React.createClass({ @@ -48,24 +49,6 @@ module.exports = React.createClass({ this.dispatcherRef = dis.register(this.onAction); }, - componentDidUpdate: function() { - // Just putting a script tag into the returned jsx doesn't work, annoyingly, - // so we do this instead. - var self = this; - if (this.refs.recaptchaContainer) { - console.log("Loading recaptcha script..."); - var scriptTag = document.createElement('script'); - window.mx_on_recaptcha_loaded = function() { - console.log("Loaded recaptcha script."); - self.props.registerLogic.tellStage("m.login.recaptcha", "loaded"); - }; - scriptTag.setAttribute( - 'src', global.location.protocol+"//www.google.com/recaptcha/api.js?onload=mx_on_recaptcha_loaded&render=explicit" - ); - this.refs.recaptchaContainer.appendChild(scriptTag); - } - }, - componentWillUnmount: function() { dis.unregister(this.dispatcherRef); }, @@ -96,6 +79,14 @@ module.exports = React.createClass({ // no matter, we'll grab it direct response = self.props.registerLogic.getCredentials(); } + if (!response || !response.user_id || !response.access_token) { + console.error("Final response is missing keys."); + self.setState({ + errorText: "There was a problem processing the response." + }); + return; + } + // TODO: do post-register stuff self.props.onLoggedIn({ userId: response.user_id, homeserverUrl: self.props.registerLogic.getHomeserverUrl(), @@ -134,6 +125,11 @@ module.exports = React.createClass({ }); }, + onCaptchaLoaded: function() { + this.props.registerLogic.tellStage("m.login.recaptcha", "loaded"); + }, + + // TODO: I wonder if this should actually be a different component... _getPostRegisterJsx: function() { var ChangeDisplayName = sdk.getComponent('molecules.ChangeDisplayName'); var ChangeAvatar = sdk.getComponent('molecules.ChangeAvatar'); @@ -174,10 +170,7 @@ module.exports = React.createClass({ break; case "Register.STEP_m.login.recaptcha": registerStep = ( -
- This Home Server would like to make sure you are not a robot -
-
+ ); break; default: diff --git a/src/skins/vector/views/pages/MatrixChat.js b/src/skins/vector/views/pages/MatrixChat.js index 6a5031db3d..7198ec9471 100644 --- a/src/skins/vector/views/pages/MatrixChat.js +++ b/src/skins/vector/views/pages/MatrixChat.js @@ -187,8 +187,8 @@ module.exports = React.createClass({ /> ); */ var registerLogic = new Signup.Register( - config.default_hs_url, config.default_is_url - ); + 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);