From b127c3043601c5a76ed98f52829439f305a619c1 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 12 Nov 2015 15:15:00 +0000 Subject: [PATCH] Implement logging in via password --- src/Signup.js | 41 +++++++++++++++++++++++++++++ src/controllers/pages/MatrixChat.js | 10 +++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Signup.js b/src/Signup.js index 06b70fb7f5..ba91ff60b0 100644 --- a/src/Signup.js +++ b/src/Signup.js @@ -1,6 +1,7 @@ "use strict"; var MatrixClientPeg = require("./MatrixClientPeg"); var dis = require("./dispatcher"); +var q = require("q"); class Register { @@ -43,6 +44,46 @@ class Login { var flowStep = this._flows[this._currentFlowIndex]; return flowStep ? flowStep.type : null; } + + loginViaPassword(username, pass) { + var self = this; + var isEmail = username.indexOf("@") > 0; + var loginParams = { + password: pass + }; + if (isEmail) { + loginParams.medium = 'email'; + loginParams.address = username; + } else { + loginParams.user = username; + } + + return MatrixClientPeg.get().login('m.login.password', loginParams).then(function(data) { + return q({ + homeserverUrl: self._hsUrl, + identityServerUrl: self._isUrl, + userId: data.user_id, + accessToken: data.access_token + }); + }, function(error) { + if (error.httpStatus == 400 && loginParams.medium) { + error.friendlyText = ( + 'This Home Server does not support login using email address.' + ); + } + else if (error.httpStatus === 403) { + error.friendlyText = ( + 'Incorrect username and/or password.' + ); + } + else { + error.friendlyText = ( + 'There was a problem logging in. (HTTP ' + error.httpStatus + ")" + ); + } + throw error; + }); + } } module.exports.Register = Register; diff --git a/src/controllers/pages/MatrixChat.js b/src/controllers/pages/MatrixChat.js index 2992d0fe3e..6be223e578 100644 --- a/src/controllers/pages/MatrixChat.js +++ b/src/controllers/pages/MatrixChat.js @@ -293,7 +293,12 @@ module.exports = { } }, - onLoggedIn: function() { + onLoggedIn: function(credentials) { + console.log("onLoggedIn => %s", credentials.userId); + MatrixClientPeg.replaceUsingAccessToken( + credentials.homeserverUrl, credentials.identityServerUrl, + credentials.userId, credentials.accessToken + ); this.setState({ screen: undefined, logged_in: true @@ -307,7 +312,8 @@ module.exports = { var cli = MatrixClientPeg.get(); var self = this; cli.on('sync', function(state) { - if (self.sdkReady || state !== "PREPARED") { return; } + console.log("MatrixClient sync state => %s", state); + if (state !== "PREPARED") { return; } self.sdkReady = true; if (self.starting_room_alias) {