From 173e80a5de0bd14a687677e05e1a81bd26e79e99 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Fri, 3 Feb 2017 14:34:24 +0000 Subject: [PATCH 1/5] Get team_token from the RTS on login Use the /login endpoint of the RTS to get the team token when the user has successfully logged in. --- src/Lifecycle.js | 16 ++++++++++++++++ src/RtsClient.js | 10 ++++++++++ src/components/structures/MatrixChat.js | 2 ++ 3 files changed, 28 insertions(+) diff --git a/src/Lifecycle.js b/src/Lifecycle.js index 493bbf12aa..43a3029fc6 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -228,6 +228,11 @@ function _restoreFromLocalStorage() { return false; } } +const RtsClient = require("./RtsClient"); +let rtsClient = null; +export function initRtsClient(url) { + rtsClient = new RtsClient(url); +} /** * Transitions to a logged-in state using the given credentials @@ -261,6 +266,17 @@ export function setLoggedIn(credentials) { } catch (e) { console.warn("Error using local storage: can't persist session!", e); } + + if (rtsClient) { + rtsClient.login(credentials.userId).then((teamToken) => { + localStorage.setItem("mx_team_token", teamToken); + }, (err) =>{ + console.error( + "Failed to get team token on login, not persisting to localStorage", + err + ); + }); + } } else { console.warn("No local storage available: can't persist session!"); } diff --git a/src/RtsClient.js b/src/RtsClient.js index ae62fb8b22..b8b51791ba 100644 --- a/src/RtsClient.js +++ b/src/RtsClient.js @@ -77,4 +77,14 @@ export default class RtsClient { } ); } + + login(userId) { + return request(this._url + '/login', + { + qs: { + user_id: userId, + }, + } + ); + } } diff --git a/src/components/structures/MatrixChat.js b/src/components/structures/MatrixChat.js index 6a84fb940f..3e57684ac2 100644 --- a/src/components/structures/MatrixChat.js +++ b/src/components/structures/MatrixChat.js @@ -210,6 +210,8 @@ module.exports = React.createClass({ window.addEventListener('resize', this.handleResize); this.handleResize(); + Lifecycle.initRtsClient(this.props.config.teamServerConfig.teamServerURL); + // the extra q() ensures that synchronous exceptions hit the same codepath as // asynchronous ones. q().then(() => { From 4f3549cc37b6e1ff3af1c679e2b94d665e490056 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Fri, 3 Feb 2017 14:42:22 +0000 Subject: [PATCH 2/5] Fix: actually get the team token --- src/Lifecycle.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Lifecycle.js b/src/Lifecycle.js index 43a3029fc6..475cffcff8 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -268,8 +268,8 @@ export function setLoggedIn(credentials) { } if (rtsClient) { - rtsClient.login(credentials.userId).then((teamToken) => { - localStorage.setItem("mx_team_token", teamToken); + rtsClient.login(credentials.userId).then((body) => { + localStorage.setItem("mx_team_token", body.team_token); }, (err) =>{ console.error( "Failed to get team token on login, not persisting to localStorage", From af19ea8bb789bd4877f0e40125b577d7cb6ac747 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Tue, 7 Feb 2017 12:01:44 +0000 Subject: [PATCH 3/5] Document login API --- src/RtsClient.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/RtsClient.js b/src/RtsClient.js index b8b51791ba..5cf2e811ad 100644 --- a/src/RtsClient.js +++ b/src/RtsClient.js @@ -78,6 +78,13 @@ export default class RtsClient { ); } + /** + * Signal to the RTS that a login has occurred and that a user requires their team's + * token. + * @param {string} userId the user ID of the user who is a member of a team. + * @returns {Promise} a promise that resolves to { team_token: 'sometoken' } upon + * success. + */ login(userId) { return request(this._url + '/login', { From 3f9f59bb73b743680ef99110ea9beb5e618f940e Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Tue, 7 Feb 2017 15:23:23 +0000 Subject: [PATCH 4/5] Import RtsClient at top --- src/Lifecycle.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Lifecycle.js b/src/Lifecycle.js index 475cffcff8..5aad0fae36 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -23,6 +23,7 @@ import UserActivity from './UserActivity'; import Presence from './Presence'; import dis from './dispatcher'; import DMRoomMap from './utils/DMRoomMap'; +import RtsClient from './RtsClient'; /** * Called at startup, to attempt to build a logged-in Matrix session. It tries @@ -228,7 +229,7 @@ function _restoreFromLocalStorage() { return false; } } -const RtsClient = require("./RtsClient"); + let rtsClient = null; export function initRtsClient(url) { rtsClient = new RtsClient(url); From 3d30db81e0d7571ae5208063320456563fb02f27 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Tue, 7 Feb 2017 15:24:57 +0000 Subject: [PATCH 5/5] Only init RTS if configured correctly --- src/components/structures/MatrixChat.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/components/structures/MatrixChat.js b/src/components/structures/MatrixChat.js index 3e57684ac2..73c8ed21d3 100644 --- a/src/components/structures/MatrixChat.js +++ b/src/components/structures/MatrixChat.js @@ -210,7 +210,11 @@ module.exports = React.createClass({ window.addEventListener('resize', this.handleResize); this.handleResize(); - Lifecycle.initRtsClient(this.props.config.teamServerConfig.teamServerURL); + if (this.props.config.teamServerConfig && + this.props.config.teamServerConfig.teamServerURL + ) { + Lifecycle.initRtsClient(this.props.config.teamServerConfig.teamServerURL); + } // the extra q() ensures that synchronous exceptions hit the same codepath as // asynchronous ones.