From 8fca6793ae667c00b47bfe3154e9cbf7d526f789 Mon Sep 17 00:00:00 2001 From: f0x Date: Thu, 4 Apr 2019 22:10:25 +0200 Subject: [PATCH] login flow to matrix backend --- components/Login.js | 105 +++++++++++++++++++++++++++++------------ public/scss/style.scss | 15 +++++- 2 files changed, 89 insertions(+), 31 deletions(-) diff --git a/components/Login.js b/components/Login.js index 81194e6..6a88d15 100644 --- a/components/Login.js +++ b/components/Login.js @@ -11,39 +11,52 @@ let login = create({ displayName: "Login", getInitialState: function() { - return {error: null} + return { + error: null, + formState: { + user: "", + pass: "", + hs: "" + }, + promptHS: false + } }, login: function() { - let user = document.getElementById("user").value - let pass = document.getElementById("pass").value - - let parts = user.split(':') + this.setState({error: ""}) + let parts = this.state.formState.user.split(':') if (parts.length != 2) { - return this.setState({error: "Please enter a full mxid, like @username:homeserver.tld"}) + return this.setState({error: "Please enter a full mxid, like username:homeserver.tld"}) } let hostname = urllib.parse("https://" + parts[1]) - getApiServer(hostname).then((hostname) => { - hostname.pathname = "" - let hs = urllib.format(hostname) + getApiServer(hostname).then((hs) => { console.log("Using API server", hs) - this.setState({apiUrl: hs}) - //this.login() + let formState = this.state.formState + formState.user = parts[0] + formState.hs = hs + this.setState({apiUrl: hs, formState: formState}) + this.doLogin() + }).catch((error) => { + console.log("LOGIN ERROR", error) + this.setState({error: error}) }) }, - login: function() { + doLogin: function() { + console.log("Logging in") + let user = this.state.formState.user + let password = this.state.formState.pass + let hs = this.state.apiUrl + let data = { - user: this.state.user, - password: this.state.password, + user: user, + password: password, type: "m.login.password", initial_device_display_name: "Neo v4", }; - let url = urllib.format(Object.assign(this.state.apiUrl, { - pathname: "/_matrix/client/r0/login" - })); + let url = hs + "/_matrix/client/r0/login" fetch(url, { body: JSON.stringify(data), @@ -53,34 +66,64 @@ let login = create({ method: 'POST', }).then((response) => response.json()) .then((responseJson) => { + console.log("got access token", responseJson) this.setState({json: responseJson}); if(responseJson.access_token != undefined) { - this.props.loginCallback() + this.props.callback(user, responseJson.access_token, hs) } }) .catch((error) => { - console.error(error); + console.error(url, error); }); }, + handleUserChange: function(e) { + let formState = this.state.formState + let user = e.target.value + formState.user = e.target.value + let parts = user.split(':') + if (parts.length == 2) { + formState.hs = parts[1] + } + this.setState({formState: formState}) + }, + + handlePassChange: function(e) { + let formState = this.state.formState + formState.pass = e.target.value + this.setState({formState: formState}) + }, + + handleHsChange: function(e) { + let formState = this.state.formState + formState.hs = e.target.value + this.setState({formState: formState}) + }, + render: function() { + let hsActive = "inactive" + if (this.state.promptHS) { + hsActive = "active" + } return (
{this.state.error != null && this.state.error}
- + - + - {this.state.promptHS && + + {this.state.promptHS ? ( <> - - + - } + ) : ( + {this.state.formState["hs"]} + )}
@@ -94,13 +137,15 @@ function getApiServer(hostname) { console.log("Checking for api server from mxid", urllib.format(hostname)) checkApi(hostname).then(() => { // Hostname is a valid api server - resolve(hostname) + hostname.pathname = "" + resolve(urllib.format(hostname)) }).catch(() => { console.log("trying .well-known") tryWellKnown(hostname).then((hostname) => { console.log("got .well-known host", hostname) + resolve(hostname) }).catch((err) => { - console.log("Fatal error trying to get API host", err) + reject("Fatal error trying to get API host") }) }) }) @@ -131,9 +176,11 @@ function tryWellKnown(host) { .then((response) => { if (response.status != 200) { console.log("no well-known in use") - reject() + reject("No homeserver found") } return response + }).catch((error) => { + reject("can't fetch .well-known") }) .then((response) => response.json()) .then((json) => { @@ -144,7 +191,7 @@ function tryWellKnown(host) { }) .catch((err) => { console.log("Error in json", err) - reject() + reject("Error while parsing .well-known") }) }) } diff --git a/public/scss/style.scss b/public/scss/style.scss index dcaad54..e5e8d9a 100644 --- a/public/scss/style.scss +++ b/public/scss/style.scss @@ -57,7 +57,7 @@ body { justify-content: center; flex-direction: column; - input, label, button { + input, label, button, span { font-size: 140%; } @@ -71,6 +71,7 @@ body { .error { color: $red; + font-size: 140%; } .login { @@ -79,8 +80,9 @@ body { grid-template-columns: 1fr 1fr; $blue: #5294e2; + $grey: #999; - label, input, button { + label, input, button, span { margin: 0.3em; padding: 0.3em; } @@ -90,6 +92,15 @@ body { justify-self: left; } + label.inactive { + background: $grey; + } + + span { + display: inline-block; + border: 0.05rem solid $grey; + } + input { border: 0.1rem solid $blue; background: transparent;