Poll for email validation once the validation email has been sent, and continue with the registration process if/when it succeeds.

Fixes https://github.com/vector-im/vector-web/issues/1027
Requires https://github.com/matrix-org/synapse/pull/650 and https://github.com/matrix-org/synapse/pull/649
This commit is contained in:
David Baker 2016-03-16 19:42:52 +00:00
parent 81674684bb
commit c9ad3705d9
2 changed files with 35 additions and 20 deletions

View file

@ -118,7 +118,7 @@ class Register extends Signup {
return this._tryRegister(); return this._tryRegister();
} }
_tryRegister(authDict) { _tryRegister(authDict, poll_for_success) {
var self = this; var self = this;
var bindEmail; var bindEmail;
@ -137,25 +137,32 @@ class Register extends Signup {
self.setStep("COMPLETE"); self.setStep("COMPLETE");
return result; // contains the credentials return result; // contains the credentials
}, function(error) { }, function(error) {
if (error.httpStatus === 401 && error.data && error.data.flows) { if (error.httpStatus === 401) {
self.data = error.data || {}; if (error.data && error.data.flows) {
var flow = self.chooseFlow(error.data.flows); self.params.sessionId = error.data.session;
self.data = error.data || {};
var flow = self.chooseFlow(error.data.flows);
if (flow) { if (flow) {
console.log("Active flow => %s", JSON.stringify(flow)); console.log("Active flow => %s", JSON.stringify(flow));
var flowStage = self.firstUncompletedStage(flow); var flowStage = self.firstUncompletedStage(flow);
return self.startStage(flowStage); if (flowStage != self.activeStage) {
return self.startStage(flowStage);
}
}
} }
else { if (poll_for_success) {
throw new Error("Unable to register - missing email address?"); return q.delay(5000).then(function() {
return self._tryRegister(authDict, poll_for_success);
});
} else {
throw new Error("Authorisation failed!");
} }
} else { } else {
if (error.errcode === 'M_USER_IN_USE') { if (error.errcode === 'M_USER_IN_USE') {
throw new Error("Username in use"); throw new Error("Username in use");
} else if (error.errcode == 'M_INVALID_USERNAME') { } else if (error.errcode == 'M_INVALID_USERNAME') {
throw new Error("User names may only contain alphanumeric characters, underscores or dots!"); 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) { } else if (error.httpStatus >= 400 && error.httpStatus < 500) {
throw new Error(`Registration failed! (${error.httpStatus})`); throw new Error(`Registration failed! (${error.httpStatus})`);
} else if (error.httpStatus >= 500 && error.httpStatus < 600) { } else if (error.httpStatus >= 500 && error.httpStatus < 600) {
@ -200,7 +207,7 @@ class Register extends Signup {
return stage.complete().then(function(request) { return stage.complete().then(function(request) {
if (request.auth) { if (request.auth) {
console.log("Stage %s is returning an auth dict", stageName); 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 { else {
// never resolve the promise chain. This is for things like email auth // never resolve the promise chain. This is for things like email auth

View file

@ -130,12 +130,15 @@ class EmailIdentityStage extends Stage {
var isLocation = document.createElement('a'); var isLocation = document.createElement('a');
isLocation.href = this.signupInstance.getIdentityServerUrl(); isLocation.href = this.signupInstance.getIdentityServerUrl();
var clientSecret = this.clientSecret || this.signupInstance.params.clientSecret;
var sid = this.sid || this.signupInstance.params.idSid;
return q({ return q({
auth: { auth: {
type: 'm.login.email.identity', type: 'm.login.email.identity',
threepid_creds: { threepid_creds: {
sid: this.signupInstance.params.idSid, sid: sid,
client_secret: this.signupInstance.params.clientSecret, client_secret: clientSecret,
id_server: isLocation.host id_server: isLocation.host
} }
} }
@ -155,10 +158,10 @@ class EmailIdentityStage extends Stage {
return this._completeVerify(); return this._completeVerify();
} }
var clientSecret = this.client.generateClientSecret(); this.clientSecret = this.client.generateClientSecret();
var nextLink = this.signupInstance.params.registrationUrl + var nextLink = this.signupInstance.params.registrationUrl +
'?client_secret=' + '?client_secret=' +
encodeURIComponent(clientSecret) + encodeURIComponent(this.clientSecret) +
"&hs_url=" + "&hs_url=" +
encodeURIComponent(this.signupInstance.getHomeserverUrl()) + encodeURIComponent(this.signupInstance.getHomeserverUrl()) +
"&is_url=" + "&is_url=" +
@ -166,13 +169,18 @@ class EmailIdentityStage extends Stage {
"&session_id=" + "&session_id=" +
encodeURIComponent(this.signupInstance.getServerData().session); encodeURIComponent(this.signupInstance.getServerData().session);
var self = this;
return this.client.requestEmailToken( return this.client.requestEmailToken(
this.signupInstance.email, this.signupInstance.email,
clientSecret, this.clientSecret,
1, // TODO: Multiple send attempts? 1, // TODO: Multiple send attempts?
nextLink nextLink
).then(function(response) { ).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) { }, function(error) {
console.error(error); console.error(error);
var e = { var e = {
@ -193,4 +201,4 @@ module.exports = {
[DummyStage.TYPE]: DummyStage, [DummyStage.TYPE]: DummyStage,
[RecaptchaStage.TYPE]: RecaptchaStage, [RecaptchaStage.TYPE]: RecaptchaStage,
[EmailIdentityStage.TYPE]: EmailIdentityStage [EmailIdentityStage.TYPE]: EmailIdentityStage
}; };