diff --git a/src/Signup.js b/src/Signup.js index fbc2a09634..0f7403ef2c 100644 --- a/src/Signup.js +++ b/src/Signup.js @@ -118,7 +118,7 @@ class Register extends Signup { return this._tryRegister(); } - _tryRegister(authDict) { + _tryRegister(authDict, poll_for_success) { var self = this; var bindEmail; @@ -137,25 +137,32 @@ class Register extends Signup { self.setStep("COMPLETE"); return result; // contains the credentials }, function(error) { - if (error.httpStatus === 401 && error.data && error.data.flows) { - self.data = error.data || {}; - var flow = self.chooseFlow(error.data.flows); + if (error.httpStatus === 401) { + if (error.data && error.data.flows) { + self.params.sessionId = error.data.session; + self.data = error.data || {}; + var flow = self.chooseFlow(error.data.flows); - if (flow) { - console.log("Active flow => %s", JSON.stringify(flow)); - var flowStage = self.firstUncompletedStage(flow); - return self.startStage(flowStage); + if (flow) { + console.log("Active flow => %s", JSON.stringify(flow)); + var flowStage = self.firstUncompletedStage(flow); + if (flowStage != self.activeStage) { + return self.startStage(flowStage); + } + } } - else { - throw new Error("Unable to register - missing email address?"); + if (poll_for_success) { + return q.delay(5000).then(function() { + return self._tryRegister(authDict, poll_for_success); + }); + } else { + throw new Error("Authorisation failed!"); } } else { if (error.errcode === 'M_USER_IN_USE') { throw new Error("Username in use"); } else if (error.errcode == 'M_INVALID_USERNAME') { throw new Error("User names may only contain alphanumeric characters, underscores or dots!"); - } else if (error.httpStatus == 401) { - throw new Error("Authorisation failed!"); } else if (error.httpStatus >= 400 && error.httpStatus < 500) { throw new Error(`Registration failed! (${error.httpStatus})`); } else if (error.httpStatus >= 500 && error.httpStatus < 600) { @@ -200,7 +207,7 @@ class Register extends Signup { return stage.complete().then(function(request) { if (request.auth) { console.log("Stage %s is returning an auth dict", stageName); - return self._tryRegister(request.auth); + return self._tryRegister(request.auth, request.poll_for_success); } else { // never resolve the promise chain. This is for things like email auth diff --git a/src/SignupStages.js b/src/SignupStages.js index 738732b9e2..1c5c48ddd6 100644 --- a/src/SignupStages.js +++ b/src/SignupStages.js @@ -130,12 +130,15 @@ class EmailIdentityStage extends Stage { var isLocation = document.createElement('a'); isLocation.href = this.signupInstance.getIdentityServerUrl(); + var clientSecret = this.clientSecret || this.signupInstance.params.clientSecret; + var sid = this.sid || this.signupInstance.params.idSid; + return q({ auth: { type: 'm.login.email.identity', threepid_creds: { - sid: this.signupInstance.params.idSid, - client_secret: this.signupInstance.params.clientSecret, + sid: sid, + client_secret: clientSecret, id_server: isLocation.host } } @@ -155,10 +158,10 @@ class EmailIdentityStage extends Stage { return this._completeVerify(); } - var clientSecret = this.client.generateClientSecret(); + this.clientSecret = this.client.generateClientSecret(); var nextLink = this.signupInstance.params.registrationUrl + '?client_secret=' + - encodeURIComponent(clientSecret) + + encodeURIComponent(this.clientSecret) + "&hs_url=" + encodeURIComponent(this.signupInstance.getHomeserverUrl()) + "&is_url=" + @@ -166,13 +169,18 @@ class EmailIdentityStage extends Stage { "&session_id=" + encodeURIComponent(this.signupInstance.getServerData().session); + var self = this; return this.client.requestEmailToken( this.signupInstance.email, - clientSecret, + this.clientSecret, 1, // TODO: Multiple send attempts? nextLink ).then(function(response) { - return {}; // don't want to make a request + self.sid = response.sid; + return self._completeVerify(); + }).then(function(request) { + request.poll_for_success = true; + return request; }, function(error) { console.error(error); var e = { @@ -193,4 +201,4 @@ module.exports = { [DummyStage.TYPE]: DummyStage, [RecaptchaStage.TYPE]: RecaptchaStage, [EmailIdentityStage.TYPE]: EmailIdentityStage -}; \ No newline at end of file +};