Move rehydration of MatrixClients from MatrixClientPeg to SessionLoader
This means that we don't create a spurious MatrixClient which is thrown away by the SessionLoader (whilst still ensuring that the rehydrated matrixclient follows the same code path as matrixclients created at other points in the session load process).pull/21833/head
parent
e0f71977b4
commit
3922f6a1b7
|
@ -88,9 +88,7 @@ export function loadSession(opts) {
|
|||
return q();
|
||||
}
|
||||
|
||||
if (MatrixClientPeg.get() && MatrixClientPeg.get().credentials) {
|
||||
console.log("Using existing credentials");
|
||||
setLoggedIn(MatrixClientPeg.getCredentials());
|
||||
if (_restoreFromLocalStorage()) {
|
||||
return q();
|
||||
}
|
||||
|
||||
|
@ -120,6 +118,39 @@ function _registerAsGuest(hsUrl, isUrl) {
|
|||
});
|
||||
}
|
||||
|
||||
// returns true if a session is found in localstorage
|
||||
function _restoreFromLocalStorage() {
|
||||
if (!localStorage) {
|
||||
return false;
|
||||
}
|
||||
const hs_url = localStorage.getItem("mx_hs_url");
|
||||
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");
|
||||
|
||||
let is_guest;
|
||||
if (localStorage.getItem("mx_is_guest") !== null) {
|
||||
is_guest = localStorage.getItem("mx_is_guest") === "true";
|
||||
} else {
|
||||
// legacy key name
|
||||
is_guest = localStorage.getItem("matrix-is-guest") === "true";
|
||||
}
|
||||
|
||||
if (access_token && user_id && hs_url) {
|
||||
console.log("Restoring session for %s", user_id);
|
||||
setLoggedIn({
|
||||
userId: user_id,
|
||||
accessToken: access_token,
|
||||
homeserverUrl: hs_url,
|
||||
identityServerUrl: is_url,
|
||||
guest: is_guest,
|
||||
});
|
||||
return true;
|
||||
} else {
|
||||
console.log("No previous session found.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transitions to a logged-in state using the given credentials
|
||||
|
|
|
@ -135,31 +135,6 @@ class MatrixClientPeg {
|
|||
};
|
||||
}
|
||||
|
||||
tryRestore() {
|
||||
if (localStorage) {
|
||||
const hs_url = localStorage.getItem("mx_hs_url");
|
||||
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");
|
||||
|
||||
let is_guest;
|
||||
if (localStorage.getItem("mx_is_guest") !== null) {
|
||||
is_guest = localStorage.getItem("mx_is_guest") === "true";
|
||||
} else {
|
||||
// legacy key name
|
||||
is_guest = localStorage.getItem("matrix-is-guest") === "true";
|
||||
}
|
||||
|
||||
if (access_token && user_id && hs_url) {
|
||||
console.log("Restoring session for %s", user_id);
|
||||
this._createClient(hs_url, is_url, user_id, access_token);
|
||||
this.matrixClient.setGuest(is_guest);
|
||||
} else {
|
||||
console.log("Session not found.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_createClient(hs_url, is_url, user_id, access_token, isGuest) {
|
||||
var opts = {
|
||||
baseUrl: hs_url,
|
||||
|
@ -186,6 +161,5 @@ class MatrixClientPeg {
|
|||
|
||||
if (!global.mxMatrixClientPeg) {
|
||||
global.mxMatrixClientPeg = new MatrixClientPeg();
|
||||
global.mxMatrixClientPeg.tryRestore();
|
||||
}
|
||||
module.exports = global.mxMatrixClientPeg;
|
||||
|
|
Loading…
Reference in New Issue