Registration works with recaptcha

This commit is contained in:
David Baker 2015-07-14 18:46:15 +01:00
parent a7e4a2847e
commit aacc31b2ce
3 changed files with 112 additions and 32 deletions

View file

@ -38,6 +38,12 @@ module.exports = React.createClass({
mixins: [RoomViewController], mixins: [RoomViewController],
render: function() { render: function() {
if (!this.state.room) {
return (
<div />
);
}
var myUserId = MatrixClientPeg.get().credentials.userId; var myUserId = MatrixClientPeg.get().credentials.userId;
if (this.state.room.currentState.members[myUserId].membership == 'invite') { if (this.state.room.currentState.members[myUserId].membership == 'invite') {
if (this.state.joining) { if (this.state.joining) {

View file

@ -36,7 +36,7 @@ var tileTypes = {
module.exports = { module.exports = {
getInitialState: function() { getInitialState: function() {
return { return {
room: MatrixClientPeg.get().getRoom(this.props.roomId), room: this.props.roomId ? MatrixClientPeg.get().getRoom(this.props.roomId) : null,
messageCap: INITIAL_SIZE messageCap: INITIAL_SIZE
} }
}, },

View file

@ -36,6 +36,17 @@ module.exports = {
}; };
}, },
componentDidUpdate: function() {
// Just putting a script tag into the returned jsx doesn't work, annoyingly,
// so we do this instead.
if (this.refs.recaptchaContainer) {
var scriptTag = document.createElement('script');
window.mx_on_recaptcha_loaded = this.onCaptchaLoaded;
scriptTag.setAttribute('src', "https://www.google.com/recaptcha/api.js?onload=mx_on_recaptcha_loaded&render=explicit");
this.refs.recaptchaContainer.getDOMNode().appendChild(scriptTag);
}
},
setStep: function(step) { setStep: function(step) {
this.setState({ step: step, errorText: '', busy: false }); this.setState({ step: step, errorText: '', busy: false });
}, },
@ -76,6 +87,7 @@ module.exports = {
if (this.savedParams.email != '') { if (this.savedParams.email != '') {
return emailFlow; return emailFlow;
t
} else { } else {
return otherFlow; return otherFlow;
} }
@ -89,44 +101,57 @@ module.exports = {
this.setState({busy: true}); this.setState({busy: true});
var self = this; var self = this;
var email = this.refs.email.getDOMNode().value;
var username = this.refs.username.getDOMNode().value;
var password = this.refs.password.getDOMNode().value;
this.savedParams = { this.savedParams = {
email: email, email: this.refs.email.getDOMNode().value,
username: username, username: this.refs.username.getDOMNode().value,
password: password password: this.refs.password.getDOMNode().value
}; };
cli.register(username, password).done(function(result) { this.tryRegister();
self.onRegistered(); },
}, function(error) {
if (error.httpStatus == 401) { startStage: function(stageName) {
var flow = self.chooseFlow(error.data.flows); var self = this;
this.setStep('stage_'+stageName);
switch(stageName) {
case 'm.login.email.identity':
self.setState({ self.setState({
busy: false, busy: true
flows: flow,
currentStep: 1,
totalSteps: flow.stages.length+1,
flowStage: 0
}); });
self.setStep('stage_'+flow.stages[0]); var cli = MatrixClientPeg.get();
} else { this.savedParams.client_secret = cli.generarteClientSecret();
self.setStep("initial"); this.savedParams.send_attempt = 1;
self.setState({ cli.requestEmailToken(
busy: false, this.savedParams.email,
errorText: 'Unable to contact the given Home Server' this.savedParams.client_secret,
this.savedParams.send_attempt
).done(function(response) {
self.setState({
busy: false,
});
self.setStep('stage_m.login.email.identity');
}, function(error) {
self.setState({
busy: false,
errorText: 'Unable to contact the given Home Server'
});
}); });
} break;
}); case 'm.login.recaptcha':
if (!this.authParams || !this.authParams['m.login.recaptcha'].public_key) {
this.setState({
errorText: "This server has not supplied enough information for Recaptcha authentication"
});
}
break;
}
}, },
onRegistered: function(user_id, access_token) { onRegistered: function(user_id, access_token) {
MatrixClientPeg.replace(Matrix.createClient({ MatrixClientPeg.replace(Matrix.createClient({
baseUrl: this.state.hs_url, baseUrl: this.state.hs_url,
userId: data.user_id, userId: user_id,
accessToken: data.access_token accessToken: access_token
})); }));
var localStorage = window.localStorage; var localStorage = window.localStorage;
if (localStorage) { if (localStorage) {
@ -136,8 +161,8 @@ module.exports = {
} else { } else {
console.warn("No local storage available: can't persist session!"); console.warn("No local storage available: can't persist session!");
} }
if (that.props.onLoggedIn) { if (this.props.onLoggedIn) {
that.props.onLoggedIn(); this.props.onLoggedIn();
} }
}, },
@ -164,13 +189,62 @@ module.exports = {
); );
case 'stage_m.login.recaptcha': case 'stage_m.login.recaptcha':
return ( return (
<div> <div ref="recaptchaContainer">
This is the recaptcha stage. Sucks, doesn't it. This Home Server would like to make sure you're not a robot
<div id="mx_recaptcha"></div>
</div> </div>
); );
} }
}, },
onCaptchaLoaded: function() {
if (this.refs.recaptchaContainer) {
var sitekey = this.authParams['m.login.recaptcha'].public_key;
global.grecaptcha.render('mx_recaptcha', {
'sitekey': sitekey,
'callback': this.onCaptchaDone
});
}
},
onCaptchaDone: function(captcha_response) {
this.tryRegister({
type: 'm.login.recaptcha',
response: captcha_response
});
},
tryRegister: function(auth) {
var self = this;
MatrixClientPeg.get().register(
this.savedParams.username,
this.savedParams.password,
this.authSessionId,
auth
).done(function(result) {
self.onRegistered(result.user_id, result.access_token);
}, function(error) {
if (error.httpStatus == 401) {
self.authParams = error.data.params;
var flow = self.chooseFlow(error.data.flows);
self.setState({
busy: false,
flows: flow,
currentStep: 1,
totalSteps: flow.stages.length+1,
flowStage: 0
});
self.startStage(flow.stages[0]);
} else {
self.setStep("initial");
self.setState({
busy: false,
errorText: 'Unable to contact the given Home Server'
});
}
});
},
showLogin: function() { showLogin: function() {
dis.dispatch({ dis.dispatch({
action: 'start_login' action: 'start_login'