diff --git a/src/Lifecycle.js b/src/Lifecycle.js index 6403cd6d4d..ee82a8890a 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -134,6 +134,7 @@ function _loginWithToken(queryParams, defaultDeviceDisplayName) { console.log("Logged in with token"); setLoggedIn({ userId: data.user_id, + deviceId: data.device_id, accessToken: data.access_token, homeserverUrl: queryParams.homeserver, identityServerUrl: queryParams.identityServer, @@ -164,6 +165,7 @@ function _registerAsGuest(hsUrl, isUrl, defaultDeviceDisplayName) { console.log("Registered as guest: %s", creds.user_id); setLoggedIn({ userId: creds.user_id, + deviceId: creds.device_id, accessToken: creds.access_token, homeserverUrl: hsUrl, identityServerUrl: isUrl, @@ -183,6 +185,7 @@ function _restoreFromLocalStorage() { const is_url = localStorage.getItem("mx_is_url") || 'https://matrix.org'; const access_token = localStorage.getItem("mx_access_token"); const user_id = localStorage.getItem("mx_user_id"); + const device_id = localStorage.getItem("mx_device_id"); let is_guest; if (localStorage.getItem("mx_is_guest") !== null) { @@ -196,6 +199,7 @@ function _restoreFromLocalStorage() { console.log("Restoring session for %s", user_id); setLoggedIn({ userId: user_id, + deviceId: device_id, accessToken: access_token, homeserverUrl: hs_url, identityServerUrl: is_url, @@ -223,10 +227,19 @@ export function setLoggedIn(credentials) { try { localStorage.setItem("mx_hs_url", credentials.homeserverUrl); localStorage.setItem("mx_is_url", credentials.identityServerUrl); - localStorage.setItem("mx_user_id", credentials.userId); localStorage.setItem("mx_access_token", credentials.accessToken); localStorage.setItem("mx_is_guest", JSON.stringify(credentials.guest)); + + // if we didn't get a deviceId from the login, leave mx_device_id unset, + // rather than setting it to "undefined". + // + // (in this case MatrixClient doesn't bother with the crypto stuff + // - that's fine for us). + if (credentials.deviceId) { + localStorage.setItem("mx_device_id", credentials.deviceId); + } + console.log("Session persisted for %s", credentials.userId); } catch (e) { console.warn("Error using local storage: can't persist session!", e); diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js index e22990a850..190181c875 100644 --- a/src/MatrixClientPeg.js +++ b/src/MatrixClientPeg.js @@ -21,21 +21,11 @@ import utils from 'matrix-js-sdk/lib/utils'; const localStorage = window.localStorage; -function deviceId() { - // XXX: is Math.random()'s deterministicity a problem here? - var id = Math.floor(Math.random()*16777215).toString(16); - id = "W" + "000000".substring(id.length) + id; - if (localStorage) { - id = localStorage.getItem("mx_device_id") || id; - localStorage.setItem("mx_device_id", id); - } - return id; -} - interface MatrixClientCreds { homeserverUrl: string, identityServerUrl: string, userId: string, + deviceId: string, accessToken: string, guest: boolean, } @@ -87,6 +77,7 @@ class MatrixClientPeg { homeserverUrl: this.matrixClient.baseUrl, identityServerUrl: this.matrixClient.idBaseUrl, userId: this.matrixClient.credentials.userId, + deviceId: this.matrixClient.getDeviceId(), accessToken: this.matrixClient.getAccessToken(), guest: this.matrixClient.isGuest(), }; @@ -98,12 +89,12 @@ class MatrixClientPeg { idBaseUrl: creds.identityServerUrl, accessToken: creds.accessToken, userId: creds.userId, + deviceId: creds.deviceId, timelineSupport: true, }; if (localStorage) { opts.sessionStore = new Matrix.WebStorageSessionStore(localStorage); - opts.deviceId = deviceId(); } this.matrixClient = Matrix.createClient(opts); diff --git a/src/Signup.js b/src/Signup.js index 9eb19d0702..1ac92f3218 100644 --- a/src/Signup.js +++ b/src/Signup.js @@ -336,6 +336,7 @@ class Login extends Signup { }).then((creds) => { return { userId: creds.user_id, + deviceId: creds.device_id, accessToken: creds.access_token, homeserverUrl: this._hsUrl, identityServerUrl: this._isUrl, @@ -371,6 +372,7 @@ class Login extends Signup { homeserverUrl: self._hsUrl, identityServerUrl: self._isUrl, userId: data.user_id, + deviceId: data.device_id, accessToken: data.access_token }); }, function(error) { @@ -394,6 +396,7 @@ class Login extends Signup { homeserverUrl: self._fallbackHsUrl, identityServerUrl: self._isUrl, userId: data.user_id, + deviceId: data.device_id, accessToken: data.access_token }); }, function(fallback_error) { diff --git a/src/components/structures/login/Registration.js b/src/components/structures/login/Registration.js index 84bfbe9834..0f1b6d331f 100644 --- a/src/components/structures/login/Registration.js +++ b/src/components/structures/login/Registration.js @@ -159,6 +159,7 @@ module.exports = React.createClass({ } self.props.onLoggedIn({ userId: response.user_id, + deviceId: response.device_id, homeserverUrl: self.registerLogic.getHomeserverUrl(), identityServerUrl: self.registerLogic.getIdentityServerUrl(), accessToken: response.access_token @@ -284,7 +285,7 @@ module.exports = React.createClass({ var returnToAppJsx; if (this.props.onCancelClick) { - returnToAppJsx = + returnToAppJsx = Return to app