Merge pull request #404 from matrix-org/dbkr/fix_settings_persistence

Fix settings resetting on refresh
pull/21833/head
Richard van der Hoff 2016-08-10 18:19:18 +01:00 committed by GitHub
commit bb3a0ff08a
2 changed files with 21 additions and 25 deletions

View File

@ -161,6 +161,26 @@ export function setLoggedIn(credentials) {
console.log("setLoggedIn => %s (guest=%s) hs=%s", console.log("setLoggedIn => %s (guest=%s) hs=%s",
credentials.userId, credentials.guest, credentials.userId, credentials.guest,
credentials.homeserverUrl); credentials.homeserverUrl);
// persist the session
if (localStorage) {
try {
localStorage.setItem("mx_hs_url", hs_url);
localStorage.setItem("mx_is_url", is_url);
if (user_id !== undefined && access_token !== undefined) {
localStorage.setItem("mx_user_id", user_id);
localStorage.setItem("mx_access_token", access_token);
localStorage.setItem("mx_is_guest", JSON.stringify(isGuest));
console.log("Session persisted for %s", user_id);
}
} catch (e) {
console.warn("Error using local storage: can't persist session!", e);
}
} else {
console.warn("No local storage available: can't persist session!");
}
MatrixClientPeg.replaceUsingCreds(credentials); MatrixClientPeg.replaceUsingCreds(credentials);
dis.dispatch({action: 'on_logged_in'}); dis.dispatch({action: 'on_logged_in'});
@ -225,6 +245,7 @@ export function onLoggedOut() {
// preserve our HS & IS URLs for convenience // preserve our HS & IS URLs for convenience
// N.B. we cache them in hsUrl/isUrl and can't really inline them // N.B. we cache them in hsUrl/isUrl and can't really inline them
// as getCurrentHsUrl() may call through to localStorage. // as getCurrentHsUrl() may call through to localStorage.
// NB. We do clear the device ID (as well as all the settings)
if (hsUrl) window.localStorage.setItem("mx_hs_url", hsUrl); if (hsUrl) window.localStorage.setItem("mx_hs_url", hsUrl);
if (isUrl) window.localStorage.setItem("mx_is_url", isUrl); if (isUrl) window.localStorage.setItem("mx_is_url", isUrl);
} }

View File

@ -97,32 +97,7 @@ class MatrixClientPeg {
} }
_replaceClient(hs_url, is_url, user_id, access_token, isGuest) { _replaceClient(hs_url, is_url, user_id, access_token, isGuest) {
if (localStorage) {
try {
localStorage.clear();
} catch (e) {
console.warn("Error clearing local storage", e);
}
}
this._createClient(hs_url, is_url, user_id, access_token, isGuest); this._createClient(hs_url, is_url, user_id, access_token, isGuest);
if (localStorage) {
try {
localStorage.setItem("mx_hs_url", hs_url);
localStorage.setItem("mx_is_url", is_url);
if (user_id !== undefined && access_token !== undefined) {
localStorage.setItem("mx_user_id", user_id);
localStorage.setItem("mx_access_token", access_token);
localStorage.setItem("mx_is_guest", JSON.stringify(isGuest));
console.log("Session persisted for %s", user_id);
}
} catch (e) {
console.warn("Error using local storage: can't persist session!", e);
}
} else {
console.warn("No local storage available: can't persist session!");
}
} }
getCredentials(): MatrixClientCreds { getCredentials(): MatrixClientCreds {