Merge pull request #399 from matrix-org/rav/refactor_session_rehydration
Move rehydration of MatrixClients from MatrixClientPeg to SessionLoader
This commit is contained in:
commit
6aa5e5a493
2 changed files with 34 additions and 29 deletions
|
@ -88,9 +88,7 @@ export function loadSession(opts) {
|
||||||
return q();
|
return q();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MatrixClientPeg.get() && MatrixClientPeg.get().credentials) {
|
if (_restoreFromLocalStorage()) {
|
||||||
console.log("Using existing credentials");
|
|
||||||
setLoggedIn(MatrixClientPeg.getCredentials());
|
|
||||||
return q();
|
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
|
* 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) {
|
_createClient(hs_url, is_url, user_id, access_token, isGuest) {
|
||||||
var opts = {
|
var opts = {
|
||||||
baseUrl: hs_url,
|
baseUrl: hs_url,
|
||||||
|
@ -186,6 +161,5 @@ class MatrixClientPeg {
|
||||||
|
|
||||||
if (!global.mxMatrixClientPeg) {
|
if (!global.mxMatrixClientPeg) {
|
||||||
global.mxMatrixClientPeg = new MatrixClientPeg();
|
global.mxMatrixClientPeg = new MatrixClientPeg();
|
||||||
global.mxMatrixClientPeg.tryRestore();
|
|
||||||
}
|
}
|
||||||
module.exports = global.mxMatrixClientPeg;
|
module.exports = global.mxMatrixClientPeg;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue