From a6dd7cc8bce71f1ea01292aa5e4d35cf05098e08 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 10 Sep 2017 19:45:45 +0900 Subject: [PATCH] Implement passwords for connection --- src/ui/connection.js | 31 ++++++++++++++++++++++++++----- src/ui/server.js | 1 + 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/ui/connection.js b/src/ui/connection.js index 8db29cf..5ac417b 100644 --- a/src/ui/connection.js +++ b/src/ui/connection.js @@ -13,21 +13,31 @@ import { import { initialize } from '..'; import { SOCKET_STATE } from '../actions/socket'; +const getURI = (uri, password) => `${uri}${password ? + `?password=${encodeURIComponent(password)}` : ''}`; + class ConnectionOverlay extends Component { constructor() { super(); + this.connect = this.connect.bind(this); const uri = localStorage.getItem("autoconnect"); - this.state = { uri: uri || "ws://127.0.0.1:8412", autoconnect: !!uri }; + const password = localStorage.getItem("password"); + this.state = { + uri: uri || "ws://127.0.0.1:8412", + password: null, + autoconnect: !!uri + }; if (uri) { - initialize(this.state.uri); + initialize(getURI(this.state.uri, this.state.password)); } } connect() { if (this.state.autoconnect) { localStorage.setItem("autoconnect", this.state.uri); + localStorage.setItem("password", this.state.password); } - initialize(this.state.uri); + initialize(getURI(this.state.uri, this.state.password)); } render() { @@ -48,7 +58,7 @@ class ConnectionOverlay extends Component { ); } - const { uri, autoconnect } = this.state; + const { uri, password, autoconnect } = this.state; return (
@@ -60,9 +70,20 @@ class ConnectionOverlay extends Component { e.keyCode === 13 && this.connect()} onChange={e => this.setState({ uri: e.target.value })} /> + + + e.keyCode === 13 && this.connect()} + onChange={e => this.setState({ password: e.target.value })} + /> + diff --git a/src/ui/server.js b/src/ui/server.js index 3c8941e..785b1a7 100644 --- a/src/ui/server.js +++ b/src/ui/server.js @@ -19,6 +19,7 @@ function Server({ server }) { className="btn btn-sm btn-outline-danger pull-right" onClick={() => { localStorage.removeItem("autoconnect"); + localStorage.removeItem("password"); ws_disconnect(); }} >Disconnect