diff --git a/src/controllers/templates/Login.js b/src/controllers/templates/Login.js index 80e5015278..4bb73aa813 100644 --- a/src/controllers/templates/Login.js +++ b/src/controllers/templates/Login.js @@ -33,57 +33,73 @@ module.exports = { }, setStep: function(step) { - this.setState({ step: step, errorText: '', busy: false }); + this.setState({ step: step, busy: false }); }, - onHSChosen: function(ev) { - ev.preventDefault(); + onHSChosen: function() { MatrixClientPeg.replaceUsingUrls( this.getHsUrl(), this.getIsUrl() ); this.setState({ hs_url: this.getHsUrl(), - is_url: this.getIsUrl() + is_url: this.getIsUrl(), }); this.setStep("fetch_stages"); var cli = MatrixClientPeg.get(); - this.setState({busy: true}); - var that = this; + this.setState({ + busy: true, + errorText: "", + }); + var self = this; cli.loginFlows().done(function(result) { - that.setState({ + self.setState({ flows: result.flows, currentStep: 1, totalSteps: result.flows.length+1 }); - that.setStep('stage_'+result.flows[0].type); + self.setStep('stage_'+result.flows[0].type); }, function(error) { - that.setStep("choose_hs"); - that.setState({errorText: 'Unable to contact the given Home Server'}); + self.setStep("choose_hs"); + self.setState({errorText: 'Unable to contact the given Home Server'}); }); }, onUserPassEntered: function(ev) { ev.preventDefault(); - this.setState({busy: true}); - var that = this; + this.setState({ + busy: true, + errorText: "", + }); + var self = this; var formVals = this.getFormVals(); - MatrixClientPeg.get().login('m.login.password', { - 'user': formVals.username, - 'password': formVals.password - }).done(function(data) { + var loginParams = { + password: formVals.password + }; + if (formVals.username.indexOf('@') > 0) { + loginParams.medium = 'email'; + loginParams.address = formVals.username; + } else { + loginParams.user = formVals.username; + } + + MatrixClientPeg.get().login('m.login.password', loginParams).done(function(data) { MatrixClientPeg.replaceUsingAccessToken( - that.state.hs_url, that.state.is_url, + self.state.hs_url, self.state.is_url, data.user_id, data.access_token ); - if (that.props.onLoggedIn) { - that.props.onLoggedIn(); + if (self.props.onLoggedIn) { + self.props.onLoggedIn(); } }, function(error) { - that.setStep("stage_m.login.password"); - that.setState({errorText: 'Login failed.'}); + self.setStep("stage_m.login.password"); + if (error.httpStatus == 400 && loginParams.medium) { + self.setState({errorText: 'This Home Server does not support login using email address.'}); + } else { + self.setState({errorText: 'Login failed.'}); + } }); },