Merge pull request #726 from matrix-org/luke/rts-set-team-token-on-login

Notify MatrixChat of teamToken after login
This commit is contained in:
Luke Barnard 2017-02-28 15:57:22 +00:00 committed by GitHub
commit f85c452e35
3 changed files with 76 additions and 66 deletions

View file

@ -277,6 +277,9 @@ export function setLoggedIn(credentials) {
credentials.userId, credentials.guest, credentials.userId, credentials.guest,
credentials.homeserverUrl); credentials.homeserverUrl);
// Resolves by default
let teamPromise = Promise.resolve(null);
// persist the session // persist the session
if (localStorage) { if (localStorage) {
try { try {
@ -300,16 +303,12 @@ export function setLoggedIn(credentials) {
console.warn("Error using local storage: can't persist session!", e); console.warn("Error using local storage: can't persist session!", e);
} }
if (rtsClient) { if (rtsClient && !credentials.guest) {
rtsClient.login(credentials.userId).then((body) => { teamPromise = rtsClient.login(credentials.userId).then((body) => {
if (body.team_token) { if (body.team_token) {
localStorage.setItem("mx_team_token", body.team_token); localStorage.setItem("mx_team_token", body.team_token);
} }
}, (err) =>{ return body.team_token;
console.error(
"Failed to get team token on login, not persisting to localStorage",
err
);
}); });
} }
} else { } else {
@ -318,7 +317,12 @@ export function setLoggedIn(credentials) {
MatrixClientPeg.replaceUsingCreds(credentials); MatrixClientPeg.replaceUsingCreds(credentials);
dis.dispatch({action: 'on_logged_in'}); teamPromise.then((teamToken) => {
dis.dispatch({action: 'on_logged_in', teamToken: teamToken});
}, (err) => {
console.warn("Failed to get team token on login", err);
dis.dispatch({action: 'on_logged_in', teamToken: null});
});
startMatrixClient(); startMatrixClient();
} }

View file

@ -519,7 +519,7 @@ module.exports = React.createClass({
this._onSetTheme(payload.value); this._onSetTheme(payload.value);
break; break;
case 'on_logged_in': case 'on_logged_in':
this._onLoggedIn(); this._onLoggedIn(payload.teamToken);
break; break;
case 'on_logged_out': case 'on_logged_out':
this._onLoggedOut(); this._onLoggedOut();
@ -695,13 +695,20 @@ module.exports = React.createClass({
/** /**
* Called when a new logged in session has started * Called when a new logged in session has started
*/ */
_onLoggedIn: function(credentials) { _onLoggedIn: function(teamToken) {
this.guestCreds = null; this.guestCreds = null;
this.notifyNewScreen(''); this.notifyNewScreen('');
this.setState({ this.setState({
screen: undefined, screen: undefined,
logged_in: true, logged_in: true,
}); });
if (teamToken) {
this._teamToken = teamToken;
this._setPage(PageTypes.HomePage);
} else if (this._is_registered) {
this._setPage(PageTypes.UserSettings);
}
}, },
/** /**
@ -718,6 +725,7 @@ module.exports = React.createClass({
currentRoomId: null, currentRoomId: null,
page_type: PageTypes.RoomDirectory, page_type: PageTypes.RoomDirectory,
}); });
this._teamToken = null;
}, },
/** /**
@ -988,23 +996,11 @@ module.exports = React.createClass({
} }
}, },
onRegistered: function(credentials) { onRegistered: function(credentials, teamToken) {
Lifecycle.setLoggedIn(credentials); // teamToken may not be truthy
// do post-registration stuff
// This now goes straight to user settings
// We use _setPage since if we wait for
// showScreen to do the dispatch loop,
// the showScreen dispatch will race with the
// sdk sync finishing and we'll probably see
// the page type still unset when the MatrixClient
// is started and show the Room Directory instead.
//this.showScreen("view_user_settings");
this._setPage(PageTypes.UserSettings);
},
onTeamMemberRegistered: function(teamToken) {
this._teamToken = teamToken; this._teamToken = teamToken;
this._setPage(PageTypes.HomePage); this._is_registered = true;
Lifecycle.setLoggedIn(credentials);
}, },
onFinishPostRegistration: function() { onFinishPostRegistration: function() {
@ -1135,7 +1131,6 @@ module.exports = React.createClass({
customIsUrl={this.getCurrentIsUrl()} customIsUrl={this.getCurrentIsUrl()}
registrationUrl={this.props.registrationUrl} registrationUrl={this.props.registrationUrl}
defaultDeviceDisplayName={this.props.defaultDeviceDisplayName} defaultDeviceDisplayName={this.props.defaultDeviceDisplayName}
onTeamMemberRegistered={this.onTeamMemberRegistered}
onLoggedIn={this.onRegistered} onLoggedIn={this.onRegistered}
onLoginClick={this.onLoginClick} onLoginClick={this.onLoginClick}
onRegisterClick={this.onRegisterClick} onRegisterClick={this.onRegisterClick}

View file

@ -16,18 +16,20 @@ limitations under the License.
'use strict'; 'use strict';
var React = require('react'); import q from 'q';
var sdk = require('../../../index'); import React from 'react';
var dis = require('../../../dispatcher');
var Signup = require("../../../Signup");
var ServerConfig = require("../../views/login/ServerConfig");
var MatrixClientPeg = require("../../../MatrixClientPeg");
var RegistrationForm = require("../../views/login/RegistrationForm");
var CaptchaForm = require("../../views/login/CaptchaForm");
var RtsClient = require("../../../RtsClient");
var MIN_PASSWORD_LENGTH = 6; import sdk from '../../../index';
import dis from '../../../dispatcher';
import Signup from '../../../Signup';
import ServerConfig from '../../views/login/ServerConfig';
import MatrixClientPeg from '../../../MatrixClientPeg';
import RegistrationForm from '../../views/login/RegistrationForm';
import CaptchaForm from '../../views/login/CaptchaForm';
import RtsClient from '../../../RtsClient';
const MIN_PASSWORD_LENGTH = 6;
/** /**
* TODO: It would be nice to make use of the InteractiveAuthEntryComponents * TODO: It would be nice to make use of the InteractiveAuthEntryComponents
@ -58,7 +60,6 @@ module.exports = React.createClass({
teamServerURL: React.PropTypes.string.isRequired, teamServerURL: React.PropTypes.string.isRequired,
}), }),
teamSelected: React.PropTypes.object, teamSelected: React.PropTypes.object,
onTeamMemberRegistered: React.PropTypes.func.isRequired,
defaultDeviceDisplayName: React.PropTypes.string, defaultDeviceDisplayName: React.PropTypes.string,
@ -206,22 +207,16 @@ module.exports = React.createClass({
}); });
return; return;
} }
self.props.onLoggedIn({
userId: response.user_id,
deviceId: response.device_id,
homeserverUrl: self.registerLogic.getHomeserverUrl(),
identityServerUrl: self.registerLogic.getIdentityServerUrl(),
accessToken: response.access_token
});
// Done regardless of `teamSelected`. People registering with non-team emails // Done regardless of `teamSelected`. People registering with non-team emails
// will just nop. The point of this being we might not have the email address // will just nop. The point of this being we might not have the email address
// that the user registered with at this stage (depending on whether this // that the user registered with at this stage (depending on whether this
// is the client they initiated registration). // is the client they initiated registration).
let trackPromise = q(null);
if (self._rtsClient) { if (self._rtsClient) {
// Track referral if self.props.referrer set, get team_token in order to // Track referral if self.props.referrer set, get team_token in order to
// retrieve team config and see welcome page etc. // retrieve team config and see welcome page etc.
self._rtsClient.trackReferral( trackPromise = self._rtsClient.trackReferral(
self.props.referrer || '', // Default to empty string = not referred self.props.referrer || '', // Default to empty string = not referred
self.registerLogic.params.idSid, self.registerLogic.params.idSid,
self.registerLogic.params.clientSecret self.registerLogic.params.clientSecret
@ -229,7 +224,6 @@ module.exports = React.createClass({
const teamToken = data.team_token; const teamToken = data.team_token;
// Store for use /w welcome pages // Store for use /w welcome pages
window.localStorage.setItem('mx_team_token', teamToken); window.localStorage.setItem('mx_team_token', teamToken);
self.props.onTeamMemberRegistered(teamToken);
self._rtsClient.getTeam(teamToken).then((team) => { self._rtsClient.getTeam(teamToken).then((team) => {
console.log( console.log(
@ -248,30 +242,25 @@ module.exports = React.createClass({
}, (err) => { }, (err) => {
console.error('Error getting team config', err); console.error('Error getting team config', err);
}); });
return teamToken;
}, (err) => { }, (err) => {
console.error('Error tracking referral', err); console.error('Error tracking referral', err);
}); });
} }
if (self.props.brand) { return trackPromise.then((teamToken) => {
MatrixClientPeg.get().getPushers().done((resp)=>{ console.info('Team token promise',teamToken);
var pushers = resp.pushers; self.props.onLoggedIn({
for (var i = 0; i < pushers.length; ++i) { userId: response.user_id,
if (pushers[i].kind == 'email') { deviceId: response.device_id,
var emailPusher = pushers[i]; homeserverUrl: self.registerLogic.getHomeserverUrl(),
emailPusher.data = { brand: self.props.brand }; identityServerUrl: self.registerLogic.getIdentityServerUrl(),
MatrixClientPeg.get().setPusher(emailPusher).done(() => { accessToken: response.access_token
console.log("Set email branding to " + self.props.brand); }, teamToken);
}, (error) => { }).then(() => {
console.error("Couldn't set email branding: " + error); self._setupPushers();
}); });
}
}
}, (error) => {
console.error("Couldn't get pushers: " + error);
});
}
}, function(err) { }, function(err) {
if (err.message) { if (err.message) {
self.setState({ self.setState({
@ -285,6 +274,28 @@ module.exports = React.createClass({
}); });
}, },
_setupPushers: function() {
if (!this.props.brand) {
return;
}
MatrixClientPeg.get().getPushers().done((resp)=>{
var pushers = resp.pushers;
for (var i = 0; i < pushers.length; ++i) {
if (pushers[i].kind == 'email') {
var emailPusher = pushers[i];
emailPusher.data = { brand: this.props.brand };
MatrixClientPeg.get().setPusher(emailPusher).done(() => {
console.log("Set email branding to " + this.props.brand);
}, (error) => {
console.error("Couldn't set email branding: " + error);
});
}
}
}, (error) => {
console.error("Couldn't get pushers: " + error);
});
},
onFormValidationFailed: function(errCode) { onFormValidationFailed: function(errCode) {
var errMsg; var errMsg;
switch (errCode) { switch (errCode) {