1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-11-11 20:22:36 +03:00

Don't use MatrixClientPeg for temporary clients

Get rid of MatrixClientPeg.replaceUsingUrls, and instead create local,
temporary MatrixClients for the unauthed steps; we therefore only use
MatrixClientPeg for logged-in clients.
This commit is contained in:
Richard van der Hoff
2016-08-11 13:50:38 +01:00
parent 3704e2c648
commit e32c325863
4 changed files with 61 additions and 97 deletions

View File

@@ -1,4 +1,7 @@
"use strict";
import Matrix from "matrix-js-sdk";
var MatrixClientPeg = require("./MatrixClientPeg");
var SignupStages = require("./SignupStages");
var dis = require("./dispatcher");
@@ -31,6 +34,17 @@ class Signup {
setIdentityServerUrl(isUrl) {
this._isUrl = isUrl;
}
/**
* Get a temporary MatrixClient, which can be used for login or register
* requests.
*/
_createTemporaryClient() {
return Matrix.createClient({
baseUrl: this._hsUrl,
idBaseUrl: this._isUrl,
});
}
}
/**
@@ -106,19 +120,11 @@ class Register extends Signup {
this.email = email;
this.username = username;
this.password = password;
// feels a bit wrong to be clobbering the global client for something we
// don't even know if it'll work, but we'll leave this here for now to
// not complicate matters further. It would be nicer to isolate this
// logic entirely from the rest of the app though.
MatrixClientPeg.replaceUsingUrls(
this._hsUrl,
this._isUrl
);
return this._tryRegister();
const client = this._createTemporaryClient();
return this._tryRegister(client);
}
_tryRegister(authDict, poll_for_success) {
_tryRegister(client, authDict, poll_for_success) {
var self = this;
var bindEmail;
@@ -129,7 +135,7 @@ class Register extends Signup {
bindEmail = true;
}
return MatrixClientPeg.get().register(
return client.register(
this.username, this.password, this.params.sessionId, authDict, bindEmail,
this.guestAccessToken
).then(function(result) {
@@ -152,7 +158,7 @@ class Register extends Signup {
console.log("Active flow => %s", JSON.stringify(flow));
var flowStage = self.firstUncompletedStage(flow);
if (flowStage != self.activeStage) {
return self.startStage(flowStage).catch(function(err) {
return self._startStage(client, flowStage).catch(function(err) {
self.setStep('START');
throw err;
});
@@ -161,7 +167,7 @@ class Register extends Signup {
}
if (poll_for_success) {
return q.delay(5000).then(function() {
return self._tryRegister(authDict, poll_for_success);
return self._tryRegister(client, authDict, poll_for_success);
});
} else {
throw new Error("Authorisation failed!");
@@ -201,7 +207,7 @@ class Register extends Signup {
return completed.indexOf(stageType) !== -1;
}
startStage(stageName) {
_startStage(client, stageName) {
var self = this;
this.setStep(`STEP_${stageName}`);
var StageClass = SignupStages[stageName];
@@ -210,12 +216,12 @@ class Register extends Signup {
throw new Error("Unknown stage: " + stageName);
}
var stage = new StageClass(MatrixClientPeg.get(), this);
var stage = new StageClass(client, this);
this.activeStage = stage;
return stage.complete().then(function(request) {
if (request.auth) {
console.log("Stage %s is returning an auth dict", stageName);
return self._tryRegister(request.auth, request.poll_for_success);
return self._tryRegister(client, request.auth, request.poll_for_success);
}
else {
// never resolve the promise chain. This is for things like email auth
@@ -263,14 +269,6 @@ class Register extends Signup {
}
recheckState() {
// feels a bit wrong to be clobbering the global client for something we
// don't even know if it'll work, but we'll leave this here for now to
// not complicate matters further. It would be nicer to isolate this
// logic entirely from the rest of the app though.
MatrixClientPeg.replaceUsingUrls(
this._hsUrl,
this._isUrl
);
// We've been given a bunch of data from a previous register step,
// this only happens for email auth currently. It's kinda ming we need
// to know this though. A better solution would be to ask the stages if
@@ -281,7 +279,8 @@ class Register extends Signup {
);
if (this.params.hasEmailInfo) {
this.registrationPromise = this.startStage(EMAIL_STAGE_TYPE);
const client = this._createTemporaryClient();
this.registrationPromise = this._startStage(client, EMAIL_STAGE_TYPE);
}
return this.registrationPromise;
}
@@ -305,15 +304,8 @@ class Login extends Signup {
getFlows() {
var self = this;
// feels a bit wrong to be clobbering the global client for something we
// don't even know if it'll work, but we'll leave this here for now to
// not complicate matters further. It would be nicer to isolate this
// logic entirely from the rest of the app though.
MatrixClientPeg.replaceUsingUrls(
this._hsUrl,
this._isUrl
);
return MatrixClientPeg.get().loginFlows().then(function(result) {
var client = this._createTemporaryClient();
return client.loginFlows().then(function(result) {
self._flows = result.flows;
self._currentFlowIndex = 0;
// technically the UI should display options for all flows for the
@@ -334,8 +326,8 @@ class Login extends Signup {
}
loginAsGuest() {
MatrixClientPeg.replaceUsingUrls(this._hsUrl, this._isUrl);
return MatrixClientPeg.get().registerGuest().then((creds) => {
var client = this._createTemporaryClient();
return client.registerGuest().then((creds) => {
return {
userId: creds.user_id,
accessToken: creds.access_token,
@@ -366,7 +358,8 @@ class Login extends Signup {
loginParams.user = username;
}
return MatrixClientPeg.get().login('m.login.password', loginParams).then(function(data) {
var client = this._createTemporaryClient();
return client.login('m.login.password', loginParams).then(function(data) {
return q({
homeserverUrl: self._hsUrl,
identityServerUrl: self._isUrl,
@@ -384,13 +377,12 @@ class Login extends Signup {
'Incorrect username and/or password.'
);
if (self._fallbackHsUrl) {
// as per elsewhere, it would be much nicer to not replace the global
// client just to try an alternate HS
MatrixClientPeg.replaceUsingUrls(
self._fallbackHsUrl,
self._isUrl
);
return MatrixClientPeg.get().login('m.login.password', loginParams).then(function(data) {
var fbClient = Matrix.createClient({
baseUrl: self._fallbackHsUrl,
idBaseUrl: this._isUrl,
});
return fbClient.login('m.login.password', loginParams).then(function(data) {
return q({
homeserverUrl: self._fallbackHsUrl,
identityServerUrl: self._isUrl,
@@ -398,11 +390,6 @@ class Login extends Signup {
accessToken: data.access_token
});
}, function(fallback_error) {
// We also have to put the default back again if it fails...
MatrixClientPeg.replaceUsingUrls(
this._hsUrl,
this._isUrl
);
// throw the original error
throw error;
});