Merge pull request #413 from matrix-org/rav/device_display_name

Set initial_device_display_name on login and register
This commit is contained in:
David Baker 2016-08-12 11:28:18 +01:00 committed by GitHub
commit 7992042526
5 changed files with 47 additions and 15 deletions

View file

@ -69,6 +69,7 @@ export function loadSession(opts) {
let enableGuest = opts.enableGuest || false;
const guestHsUrl = opts.guestHsUrl;
const guestIsUrl = opts.guestIsUrl;
const defaultDeviceDisplayName = opts.defaultDeviceDisplayName;
if (fragmentQueryParams.client_secret && fragmentQueryParams.sid) {
// this happens during email validation: the email contains a link to the
@ -87,7 +88,7 @@ export function loadSession(opts) {
if (!realQueryParams.homeserver) {
console.warn("Cannot log in with token: can't determine HS URL to use");
} else {
return _loginWithToken(realQueryParams);
return _loginWithToken(realQueryParams, defaultDeviceDisplayName);
}
}
@ -111,20 +112,25 @@ export function loadSession(opts) {
}
if (enableGuest) {
return _registerAsGuest(guestHsUrl, guestIsUrl);
return _registerAsGuest(guestHsUrl, guestIsUrl, defaultDeviceDisplayName);
}
// fall back to login screen
return q();
}
function _loginWithToken(queryParams) {
function _loginWithToken(queryParams, defaultDeviceDisplayName) {
// create a temporary MatrixClient to do the login
var client = Matrix.createClient({
baseUrl: queryParams.homeserver,
});
return client.loginWithToken(queryParams.loginToken).then(function(data) {
return client.login(
"m.login.token", {
token: queryParams.loginToken,
initial_device_display_name: defaultDeviceDisplayName,
},
).then(function(data) {
console.log("Logged in with token");
setLoggedIn({
userId: data.user_id,
@ -139,15 +145,22 @@ function _loginWithToken(queryParams) {
});
}
function _registerAsGuest(hsUrl, isUrl) {
function _registerAsGuest(hsUrl, isUrl, defaultDeviceDisplayName) {
console.log("Doing guest login on %s", hsUrl);
// TODO: we should probably de-duplicate this and Signup.Login.loginAsGuest.
// Not really sure where the right home for it is.
// create a temporary MatrixClient to do the login
var client = Matrix.createClient({
baseUrl: hsUrl,
});
return client.registerGuest().then((creds) => {
return client.registerGuest({
body: {
initial_device_display_name: defaultDeviceDisplayName,
},
}).then((creds) => {
console.log("Registered as guest: %s", creds.user_id);
setLoggedIn({
userId: creds.user_id,

View file

@ -14,9 +14,10 @@ const EMAIL_STAGE_TYPE = "m.login.email.identity";
* storage of HS/IS URLs.
*/
class Signup {
constructor(hsUrl, isUrl) {
constructor(hsUrl, isUrl, opts) {
this._hsUrl = hsUrl;
this._isUrl = isUrl;
this._defaultDeviceDisplayName = opts.defaultDeviceDisplayName;
}
getHomeserverUrl() {
@ -51,8 +52,8 @@ class Signup {
* Registration logic class
*/
class Register extends Signup {
constructor(hsUrl, isUrl) {
super(hsUrl, isUrl);
constructor(hsUrl, isUrl, opts) {
super(hsUrl, isUrl, opts);
this.setStep("START");
this.data = null; // from the server
// random other stuff (e.g. query params, NOT params from the server)
@ -135,6 +136,7 @@ class Register extends Signup {
bindEmail = true;
}
// TODO need to figure out how to send the device display name to /register.
return client.register(
this.username, this.password, this.params.sessionId, authDict, bindEmail,
this.guestAccessToken
@ -295,8 +297,8 @@ class Register extends Signup {
class Login extends Signup {
constructor(hsUrl, isUrl, fallbackHsUrl) {
super(hsUrl, isUrl);
constructor(hsUrl, isUrl, fallbackHsUrl, opts) {
super(hsUrl, isUrl, opts);
this._fallbackHsUrl = fallbackHsUrl;
this._currentFlowIndex = 0;
this._flows = [];
@ -327,7 +329,11 @@ class Login extends Signup {
loginAsGuest() {
var client = this._createTemporaryClient();
return client.registerGuest().then((creds) => {
return client.registerGuest({
body: {
initial_device_display_name: this._defaultDeviceDisplayName,
},
}).then((creds) => {
return {
userId: creds.user_id,
accessToken: creds.access_token,
@ -349,7 +355,8 @@ class Login extends Signup {
var self = this;
var isEmail = username.indexOf("@") > 0;
var loginParams = {
password: pass
password: pass,
initial_device_display_name: this._defaultDeviceDisplayName,
};
if (isEmail) {
loginParams.medium = 'email';

View file

@ -185,6 +185,7 @@ module.exports = React.createClass({
enableGuest: this.props.enableGuest,
guestHsUrl: this.getCurrentHsUrl(),
guestIsUrl: this.getCurrentIsUrl(),
defaultDisplayName: this.props.config.default_device_name,
}).done(()=>{
// stuff this through the dispatcher so that it happens
// after the on_logged_in action.
@ -1039,6 +1040,7 @@ module.exports = React.createClass({
customHsUrl={this.getCurrentHsUrl()}
customIsUrl={this.getCurrentIsUrl()}
registrationUrl={this.props.registrationUrl}
defaultDeviceDisplayName={this.props.config.default_device_name}
onLoggedIn={this.onRegistered}
onLoginClick={this.onLoginClick}
onRegisterClick={this.onRegisterClick}
@ -1065,6 +1067,7 @@ module.exports = React.createClass({
customHsUrl={this.getCurrentHsUrl()}
customIsUrl={this.getCurrentIsUrl()}
fallbackHsUrl={this.getFallbackHsUrl()}
defaultDeviceDisplayName={this.props.config.default_device_name}
onForgotPasswordClick={this.onForgotPasswordClick}
enableGuest={this.props.enableGuest}
onCancelClick={this.guestCreds ? this.onReturnToGuestClick : null}

View file

@ -44,6 +44,8 @@ module.exports = React.createClass({
// different home server without confusing users.
fallbackHsUrl: React.PropTypes.string,
defaultDeviceDisplayName: React.PropTypes.string,
// login shouldn't know or care how registration is done.
onRegisterClick: React.PropTypes.func.isRequired,
@ -136,7 +138,9 @@ module.exports = React.createClass({
var fallbackHsUrl = hsUrl == this.props.defaultHsUrl ? this.props.fallbackHsUrl : null;
var loginLogic = new Signup.Login(hsUrl, isUrl, fallbackHsUrl);
var loginLogic = new Signup.Login(hsUrl, isUrl, fallbackHsUrl, {
defaultDeviceDisplayName: this.props.defaultDeviceDisplayName,
});
this._loginLogic = loginLogic;
loginLogic.getFlows().then(function(flows) {

View file

@ -45,6 +45,9 @@ module.exports = React.createClass({
email: React.PropTypes.string,
username: React.PropTypes.string,
guestAccessToken: React.PropTypes.string,
defaultDeviceDisplayName: React.PropTypes.string,
// registration shouldn't know or care how login is done.
onLoginClick: React.PropTypes.func.isRequired,
onCancelClick: React.PropTypes.func
@ -71,7 +74,9 @@ module.exports = React.createClass({
this.dispatcherRef = dis.register(this.onAction);
// attach this to the instance rather than this.state since it isn't UI
this.registerLogic = new Signup.Register(
this.props.customHsUrl, this.props.customIsUrl
this.props.customHsUrl, this.props.customIsUrl, {
defaultDeviceDisplayName: this.props.defaultDeviceDisplayName,
}
);
this.registerLogic.setClientSecret(this.props.clientSecret);
this.registerLogic.setSessionId(this.props.sessionId);