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 (