Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #676 from matrix-org/luke/rts-team-token-login
Browse files Browse the repository at this point in the history
Get team_token from the RTS on login
  • Loading branch information
dbkr committed Feb 8, 2017
2 parents 8fea4c2 + 3d30db8 commit fe17e24
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -229,6 +230,11 @@ function _restoreFromLocalStorage() {
}
}

let rtsClient = null;
export function initRtsClient(url) {
rtsClient = new RtsClient(url);
}

/**
* Transitions to a logged-in state using the given credentials
* @param {MatrixClientCreds} credentials The credentials to use
Expand Down Expand Up @@ -261,6 +267,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((body) => {
localStorage.setItem("mx_team_token", body.team_token);
}, (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!");
}
Expand Down
17 changes: 17 additions & 0 deletions src/RtsClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,21 @@ 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',
{
qs: {
user_id: userId,
},
}
);
}
}
6 changes: 6 additions & 0 deletions src/components/structures/MatrixChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ module.exports = React.createClass({
window.addEventListener('resize', this.handleResize);
this.handleResize();

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.
q().then(() => {
Expand Down

0 comments on commit fe17e24

Please sign in to comment.