Merge pull request #436 from matrix-org/rav/handle_broken_olm

Handle broken OlmAccounts
This commit is contained in:
Richard van der Hoff 2016-09-02 11:58:40 +01:00 committed by GitHub
commit d3bc2b7df7

View file

@ -197,6 +197,7 @@ function _restoreFromLocalStorage() {
if (access_token && user_id && hs_url) { if (access_token && user_id && hs_url) {
console.log("Restoring session for %s", user_id); console.log("Restoring session for %s", user_id);
try {
setLoggedIn({ setLoggedIn({
userId: user_id, userId: user_id,
deviceId: device_id, deviceId: device_id,
@ -206,6 +207,21 @@ function _restoreFromLocalStorage() {
guest: is_guest, guest: is_guest,
}); });
return true; return true;
} catch (e) {
console.log("Unable to restore session", e);
var msg = e.message;
if (msg == "OLM.BAD_LEGACY_ACCOUNT_PICKLE") {
msg = "You need to log back in to generate and submit "
+ "end-to-end encryption keys. This is a once off; sorry "
+ "for the inconvenience.";
}
// don't leak things into the new session
_clearLocalStorage();
throw new Error("Unable to restore previous session: " + msg);
}
} else { } else {
console.log("No previous session found."); console.log("No previous session found.");
return false; return false;
@ -305,20 +321,25 @@ export function startMatrixClient() {
* a session has been logged out / ended. * a session has been logged out / ended.
*/ */
export function onLoggedOut() { export function onLoggedOut() {
if (window.localStorage) { _clearLocalStorage();
stopMatrixClient();
dis.dispatch({action: 'on_logged_out'});
}
function _clearLocalStorage() {
if (!window.localStorage) {
return;
}
const hsUrl = window.localStorage.getItem("mx_hs_url"); const hsUrl = window.localStorage.getItem("mx_hs_url");
const isUrl = window.localStorage.getItem("mx_is_url"); const isUrl = window.localStorage.getItem("mx_is_url");
window.localStorage.clear(); window.localStorage.clear();
// 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) // 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);
}
stopMatrixClient();
dis.dispatch({action: 'on_logged_out'});
} }
/** /**