1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-12-04 03:22:43 +03:00

Get Recaptcha working again. Add a backchannel for stage prodding.

Recaptcha is a special snowflake because it dynamically loads the script
and THEN renders with info from the registration request. This means we
need a back-channel for the UI component to 'tell' the stage that everything
is loaded. This Just Works which is nice.
This commit is contained in:
Kegan Dougal
2015-11-18 17:43:38 +00:00
parent 991a96cfc5
commit 3e903be73d
2 changed files with 48 additions and 8 deletions

View File

@@ -16,6 +16,10 @@ class Stage {
// Has a "message" string and an "isFatal" flag.
return q.reject("NOT IMPLEMENTED");
}
onReceiveData() {
// NOP
}
}
Stage.TYPE = "NOT IMPLEMENTED";
@@ -39,12 +43,22 @@ DummyStage.TYPE = "m.login.dummy";
class RecaptchaStage extends Stage {
constructor(matrixClient, signupInstance) {
super(RecaptchaStage.TYPE, matrixClient, signupInstance);
this.defer = q.defer();
this.publicKey = null;
}
onReceiveData(data) {
if (data !== "loaded") {
return;
}
this._attemptRender();
}
complete() {
var publicKey;
if (this.signupInstance.params['m.login.recaptcha']) {
publicKey = this.signupInstance.params['m.login.recaptcha'].public_key;
var serverParams = this.signupInstance.getServerData().params;
if (serverParams && serverParams["m.login.recaptcha"]) {
publicKey = serverParams["m.login.recaptcha"].public_key;
}
if (!publicKey) {
return q.reject({
@@ -53,12 +67,26 @@ class RecaptchaStage extends Stage {
isFatal: true
});
}
this.publicKey = publicKey;
this._attemptRender();
var defer = q.defer();
return this.defer.promise;
}
_attemptRender() {
if (!global.grecaptcha) {
console.error("grecaptcha not loaded!");
return;
}
if (!this.publicKey) {
console.error("No public key for recaptcha!");
return;
}
var self = this;
global.grecaptcha.render('mx_recaptcha', {
sitekey: publicKey,
sitekey: this.publicKey,
callback: function(response) {
return defer.resolve({
return self.defer.resolve({
auth: {
type: 'm.login.recaptcha',
response: response
@@ -66,8 +94,6 @@ class RecaptchaStage extends Stage {
});
}
});
return defer.promise;
}
}
RecaptchaStage.TYPE = "m.login.recaptcha";