diff --git a/src/Lifecycle.js b/src/Lifecycle.js index f7579cf3c0e..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; @@ -415,16 +416,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..007af6385d4 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, @@ -1828,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);