From 173e80a5de0bd14a687677e05e1a81bd26e79e99 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Fri, 3 Feb 2017 14:34:24 +0000 Subject: [PATCH] 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(() => {