Merge pull request #12 from matrix-org/markjh/cleanup-client-peg

Move all the calls to createClient inside the MatrixClientPeg
This commit is contained in:
David Baker 2015-07-20 10:11:24 -07:00
commit 6ceaee7fc0
3 changed files with 37 additions and 45 deletions

View File

@ -22,18 +22,25 @@ var Matrix = require("matrix-js-sdk");
var matrixClient = null; var matrixClient = null;
var localStorage = window.localStorage; var localStorage = window.localStorage;
function createClient(hs_url, is_url, user_id, access_token) {
var opts = {
baseUrl: hs_url,
idBaseUrl: is_url,
accessToken: access_token,
userId: user_id
};
matrixClient = Matrix.createClient(opts);
}
if (localStorage) { if (localStorage) {
var hs_url = localStorage.getItem("mx_hs_url"); var hs_url = localStorage.getItem("mx_hs_url");
var is_url = localStorage.getItem("mx_is_url") || 'https://matrix.org'; var is_url = localStorage.getItem("mx_is_url") || 'https://matrix.org';
var access_token = localStorage.getItem("mx_access_token"); var access_token = localStorage.getItem("mx_access_token");
var user_id = localStorage.getItem("mx_user_id"); var user_id = localStorage.getItem("mx_user_id");
if (access_token && user_id && hs_url) { if (access_token && user_id && hs_url) {
matrixClient = Matrix.createClient({ createClient(hs_url, is_url, user_id, access_token);
baseUrl: hs_url,
idBaseUrl: is_url,
accessToken: access_token,
userId: user_id
});
} }
} }
@ -42,15 +49,28 @@ module.exports = {
return matrixClient; return matrixClient;
}, },
replace: function(cli) {
matrixClient = cli;
},
replaceUsingUrls: function(hs_url, is_url) { replaceUsingUrls: function(hs_url, is_url) {
matrixClient = Matrix.createClient({ matrixClient = Matrix.createClient({
baseUrl: hs_url, baseUrl: hs_url,
idBaseUrl: is_url idBaseUrl: is_url
}); });
},
replaceUsingAccessToken: function(hs_url, is_url, user_id, access_token) {
createClient(hs_url, is_url, user_id, access_token);
if (localStorage) {
try {
localStorage.clear();
localStorage.setItem("mx_hs_url", hs_url);
localStorage.setItem("mx_is_url", is_url);
localStorage.setItem("mx_user_id", user_id);
localStorage.setItem("mx_access_token", access_token);
} catch (e) {
console.warn("Error using local storage: can't persist session!");
}
} else {
console.warn("No local storage available: can't persist session!");
}
} }
}; };

View File

@ -76,27 +76,10 @@ module.exports = {
'user': formVals.username, 'user': formVals.username,
'password': formVals.password 'password': formVals.password
}).done(function(data) { }).done(function(data) {
// XXX: we assume this means we're logged in, but there could be a next stage MatrixClientPeg.replaceUsingAccessToken(
MatrixClientPeg.replace(Matrix.createClient({ that.state.hs_url, that.state.is_url,
baseUrl: that.state.hs_url, data.user_id, data.access_token
idBaseUrl: that.state.is_url, );
userId: data.user_id,
accessToken: data.access_token
}));
var localStorage = window.localStorage;
if (localStorage) {
try {
localStorage.clear();
localStorage.setItem("mx_hs_url", that.state.hs_url);
localStorage.setItem("mx_is_url", that.state.is_url);
localStorage.setItem("mx_user_id", data.user_id);
localStorage.setItem("mx_access_token", data.access_token);
} catch (e) {
console.warn("Error using local storage: can't persist session!");
}
} else {
console.warn("No local storage available: can't persist session!");
}
if (that.props.onLoggedIn) { if (that.props.onLoggedIn) {
that.props.onLoggedIn(); that.props.onLoggedIn();
} }

View File

@ -259,20 +259,9 @@ module.exports = {
}, },
onRegistered: function(user_id, access_token) { onRegistered: function(user_id, access_token) {
MatrixClientPeg.replace(Matrix.createClient({ MatrixClientPeg.replaceUsingAccessToken(
baseUrl: this.state.hs_url, this.state.hs_url, this.state.is_url, user_id, access_token
idBaseUrl: this.state.is_url, );
userId: user_id,
accessToken: access_token
}));
var localStorage = window.localStorage;
if (localStorage) {
localStorage.setItem("mx_hs_url", this.state.hs_url);
localStorage.setItem("mx_user_id", user_id);
localStorage.setItem("mx_access_token", access_token);
} else {
console.warn("No local storage available: can't persist session!");
}
if (this.props.onLoggedIn) { if (this.props.onLoggedIn) {
this.props.onLoggedIn(); this.props.onLoggedIn();
} }