diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js index 7a14db607a..ee417174c9 100644 --- a/src/MatrixClientPeg.js +++ b/src/MatrixClientPeg.js @@ -3,6 +3,20 @@ var Matrix = require("matrix-js-sdk"); var matrixClient = null; +var localStorage = window.localStorage; +if (localStorage) { + var hs_url = localStorage.getItem("mx_hs_url"); + var access_token = localStorage.getItem("mx_access_token"); + var user_id = localStorage.getItem("mx_user_id"); + if (access_token && user_id && hs_url) { + matrixClient = Matrix.createClient({ + baseUrl: hs_url, + accessToken: access_token, + userId: user_id + }); + } +} + module.exports = { get: function() { return matrixClient; diff --git a/src/pages/MatrixChat.js b/src/pages/MatrixChat.js index 04ea1fd22e..01b9ebe247 100644 --- a/src/pages/MatrixChat.js +++ b/src/pages/MatrixChat.js @@ -12,7 +12,7 @@ var dis = require("../dispatcher"); module.exports = React.createClass({ getInitialState: function() { return { - logged_in: false + logged_in: !!mxCliPeg.get().credentials }; }, diff --git a/src/templates/Login.js b/src/templates/Login.js index b4bbc0a579..77518bb1f8 100644 --- a/src/templates/Login.js +++ b/src/templates/Login.js @@ -24,8 +24,10 @@ module.exports = React.createClass({ onHSChosen: function(ev) { MatrixClientPeg.replaceUsingUrl(this.refs.serverConfig.getHsUrl()); + this.setState({hs_url: this.refs.serverConfig.getHsUrl()}); this.setStep("fetch_stages"); var cli = MatrixClientPeg.get(); + this.setState({busy: true}); var that = this; cli.loginFlows().then(function(result) { that.setState({ @@ -41,11 +43,21 @@ module.exports = React.createClass({ }, onUserPassEntered: function(ev) { + this.setState({busy: true}); var that = this; MatrixClientPeg.get().login('m.login.password', { 'user': that.refs.user.getDOMNode().value, 'password': that.refs.pass.getDOMNode().value }).then(function(data) { + // XXX: we assume this means we're logged in, but there could be a next stage + var localStorage = window.localStorage; + if (localStorage) { + localStorage.setItem("mx_hs_url", that.state.hs_url); + localStorage.setItem("mx_user_id", data.user_id); + localStorage.setItem("mx_access_token", data.access_token); + } else { + console.warn("No local storage available: can't persist session!"); + } dis.dispatch({ 'action': 'logged_in' });