From 5b2328bad37cb8780e2334602b59e62ee7c585e0 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 22 Mar 2019 19:04:39 -0600 Subject: [PATCH 1/3] Log out of the guest account if coming from the welcome page This is ultimately the fix for https://github.com/vector-im/riot-web/issues/9224 however it is not really great. By not logging out of the guest account, getCurrentHsUrl() returns the wrong homeserver URL (matrix.org) instead of the discovered one. --- src/Lifecycle.js | 16 +++++++++------- src/components/structures/MatrixChat.js | 25 ++++++++++++++++++------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/Lifecycle.js b/src/Lifecycle.js index f7579cf3c0e..1fbfe4445e9 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -415,16 +415,18 @@ export function logout() { // Also we sometimes want to re-log in a guest session // if we abort the login - // use settimeout to avoid racing with react unmounting components - // which need a valid matrixclientpeg - setTimeout(()=>{ - onLoggedOut(); - }, 0); - return; + return new Promise((resolve, _reject) => { + // use settimeout to avoid racing with react unmounting components + // which need a valid matrixclientpeg + setTimeout(()=>{ + onLoggedOut(); + resolve(); + }, 0); + }); } _isLoggingOut = true; - MatrixClientPeg.get().logout().then(onLoggedOut, + return MatrixClientPeg.get().logout().then(onLoggedOut, (err) => { // Just throwing an error here is going to be very unhelpful // if you're trying to log out because your server's down and diff --git a/src/components/structures/MatrixChat.js b/src/components/structures/MatrixChat.js index 2622a6bf93b..51b834a350f 100644 --- a/src/components/structures/MatrixChat.js +++ b/src/components/structures/MatrixChat.js @@ -502,15 +502,26 @@ export default React.createClass({ startAnyRegistrationFlow(payload); break; case 'start_registration': - // This starts the full registration flow - this._startRegistration(payload.params || {}); - break; - case 'start_login': - this.setStateForNewView({ - view: VIEWS.LOGIN, + case 'start_login': { + let logoutPromise = Promise.resolve(); + if (payload.params && payload.params.logoutFirst) { + logoutPromise = Lifecycle.logout(); + } + logoutPromise.then(() => { + if (payload.action === "start_login") { + this.setStateForNewView({ + view: VIEWS.LOGIN, + }); + this.notifyNewScreen('login'); + } else if (payload.action === "start_registration") { + // This starts the full registration flow + this._startRegistration(payload.params || {}); + } else { + throw new Error("Unknown action for post-logout"); + } }); - this.notifyNewScreen('login'); break; + } case 'start_post_registration': this.setState({ view: VIEWS.POST_REGISTRATION, From a7f00e9fcdfc16df39b1691db93b923f5fc78ff1 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 22 Mar 2019 19:09:48 -0600 Subject: [PATCH 2/3] Switch the guest account over to the right homeserver after discovery In 5b2328bad37cb8780e2334602b59e62ee7c585e0 we log out of the guest account before moving on, however that doesn't solve the case of the guest account being wrong to begin with. We should still keep 5b2328bad37cb8780e2334602b59e62ee7c585e0 in the event this races, however this commit's goal is to use the right homeserver for the guest account once it has been discovered. --- src/components/structures/MatrixChat.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/components/structures/MatrixChat.js b/src/components/structures/MatrixChat.js index 51b834a350f..007af6385d4 100644 --- a/src/components/structures/MatrixChat.js +++ b/src/components/structures/MatrixChat.js @@ -1839,6 +1839,12 @@ export default React.createClass({ defaultIsUrl: isUrl, loadingDefaultHomeserver: false, }); + + // Change over the guest account, if one exists + if (MatrixClientPeg.get() && MatrixClientPeg.get().isGuest()) { + console.log("Discovered homeserver URLs - logging out current guest to use the homeserver"); + Lifecycle.logout().then(() => this._loadSession()); + } } } catch (e) { console.error(e); From 6b3bfd9a92525f88512f3920213e529158e65a0c Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 22 Mar 2019 19:17:05 -0600 Subject: [PATCH 3/3] jsdoc for the linter --- src/Lifecycle.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Lifecycle.js b/src/Lifecycle.js index 1fbfe4445e9..a5dbf79a46e 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -406,6 +406,7 @@ let _isLoggingOut = false; /** * Logs the current session out and transitions to the logged-out state + * @returns {Promise} a promise which resolves when the logout has finished. */ export function logout() { if (!MatrixClientPeg.get()) return;