From a0ecd89c94a4269d81ff2b163a82b9f767e94769 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 14 May 2019 11:44:00 +0100 Subject: [PATCH 1/3] Fix email registration: pt. 1 We look to see if there's already a user logged in and if there is, restore that session instead of logging the user in as their new account. We still set this 'is_registered' flag though, even though in that case it's not a newly registered account that's being restored, so don't set in that case. --- src/components/structures/MatrixChat.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/structures/MatrixChat.js b/src/components/structures/MatrixChat.js index 277985ba1d0..49ee1ca8726 100644 --- a/src/components/structures/MatrixChat.js +++ b/src/components/structures/MatrixChat.js @@ -1710,9 +1710,6 @@ export default React.createClass({ // returns a promise which resolves to the new MatrixClient onRegistered: function(credentials) { - // XXX: This should be in state or ideally store(s) because we risk not - // rendering the most up-to-date view of state otherwise. - this._is_registered = true; if (this.state.register_session_id) { // The user came in through an email validation link. To avoid overwriting // their session, check to make sure the session isn't someone else. @@ -1748,6 +1745,9 @@ export default React.createClass({ return MatrixClientPeg.get(); } } + // XXX: This should be in state or ideally store(s) because we risk not + // rendering the most up-to-date view of state otherwise. + this._is_registered = true; return Lifecycle.setLoggedIn(credentials); }, From c11d26d809ef42b37973db874f9408fd78a19479 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 14 May 2019 11:59:38 +0100 Subject: [PATCH 2/3] Fix email registration, pt. 2 Regressed in https://github.com/matrix-org/matrix-react-sdk/pull/2768 where we check for an existing stored account first and restore that instead if it exist, telling the user. We usually make a guest account when the user first hits the page though, so this just restored this guest account. Don't restore the account if it's just a guest account (which, as per comment, is not perfect, but is definitely better than the current behaviour). Fixes https://github.com/vector-im/riot-web/issues/9581 --- src/Lifecycle.js | 29 ++++++++++++++++--------- src/components/structures/MatrixChat.js | 8 +++++-- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/Lifecycle.js b/src/Lifecycle.js index 527394da4db..ca4c7c48f8e 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -126,6 +126,15 @@ export function getStoredSessionOwner() { return hsUrl && userId && accessToken ? userId : null; } +/** + * @returns {bool} True if the stored session is for a guest user or false if it is + * for a real user. If there is no stored session, return null. + */ +export function getStoredSessionIsGuest() { + const {hsUrl, isUrl, accessToken, userId, deviceId, isGuest} = _getLocalStorageSessionVars(); + return hsUrl && userId && accessToken ? isGuest : null; +} + /** * @param {Object} queryParams string->string map of the * query-parameters extracted from the real query-string of the starting @@ -235,7 +244,15 @@ function _getLocalStorageSessionVars() { const userId = localStorage.getItem("mx_user_id"); const deviceId = localStorage.getItem("mx_device_id"); - return {hsUrl, isUrl, accessToken, userId, deviceId}; + let isGuest; + if (localStorage.getItem("mx_is_guest") !== null) { + isGuest = localStorage.getItem("mx_is_guest") === "true"; + } else { + // legacy key name + isGuest = localStorage.getItem("matrix-is-guest") === "true"; + } + + return {hsUrl, isUrl, accessToken, userId, deviceId, isGuest}; } // returns a promise which resolves to true if a session is found in @@ -253,15 +270,7 @@ async function _restoreFromLocalStorage() { return false; } - const {hsUrl, isUrl, accessToken, userId, deviceId} = _getLocalStorageSessionVars(); - - let isGuest; - if (localStorage.getItem("mx_is_guest") !== null) { - isGuest = localStorage.getItem("mx_is_guest") === "true"; - } else { - // legacy key name - isGuest = localStorage.getItem("matrix-is-guest") === "true"; - } + const {hsUrl, isUrl, accessToken, userId, deviceId, isGuest} = _getLocalStorageSessionVars(); if (accessToken && userId && hsUrl) { console.log(`Restoring session for ${userId}`); diff --git a/src/components/structures/MatrixChat.js b/src/components/structures/MatrixChat.js index 49ee1ca8726..0b52cfa1bcb 100644 --- a/src/components/structures/MatrixChat.js +++ b/src/components/structures/MatrixChat.js @@ -1712,9 +1712,13 @@ export default React.createClass({ onRegistered: function(credentials) { if (this.state.register_session_id) { // The user came in through an email validation link. To avoid overwriting - // their session, check to make sure the session isn't someone else. + // their session, check to make sure the session isn't someone else, and + // isn't a guest user since we'll usually have set a guest user session before + // starting the registration process. This isn't perfect since it's possible + // the user had a separate guest session they didn't actually mean to replace. const sessionOwner = Lifecycle.getStoredSessionOwner(); - if (sessionOwner && sessionOwner !== credentials.userId) { + const sessionIsGuest = Lifecycle.getStoredSessionIsGuest(); + if (sessionOwner && !sessionIsGuest && sessionOwner !== credentials.userId) { console.log( `Found a session for ${sessionOwner} but ${credentials.userId} is trying to verify their ` + `email address. Restoring the session for ${sessionOwner} with warning.`, From 7ac82a23ab62eb01118e885c011dae907e4034de Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 14 May 2019 12:11:07 +0100 Subject: [PATCH 3/3] Yes yes, eslint --- src/Lifecycle.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Lifecycle.js b/src/Lifecycle.js index ca4c7c48f8e..0e2389fd1c1 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -131,8 +131,8 @@ export function getStoredSessionOwner() { * for a real user. If there is no stored session, return null. */ export function getStoredSessionIsGuest() { - const {hsUrl, isUrl, accessToken, userId, deviceId, isGuest} = _getLocalStorageSessionVars(); - return hsUrl && userId && accessToken ? isGuest : null; + const sessVars = _getLocalStorageSessionVars(); + return sessVars.hsUrl && sessVars.userId && sessVars.accessToken ? sessVars.isGuest : null; } /**