Implement passwords for connection

This commit is contained in:
Drew DeVault 2017-09-10 19:45:45 +09:00
parent 51b1f30729
commit a6dd7cc8bc
2 changed files with 27 additions and 5 deletions

View File

@ -13,21 +13,31 @@ import {
import { initialize } from '..'; import { initialize } from '..';
import { SOCKET_STATE } from '../actions/socket'; import { SOCKET_STATE } from '../actions/socket';
const getURI = (uri, password) => `${uri}${password ?
`?password=${encodeURIComponent(password)}` : ''}`;
class ConnectionOverlay extends Component { class ConnectionOverlay extends Component {
constructor() { constructor() {
super(); super();
this.connect = this.connect.bind(this);
const uri = localStorage.getItem("autoconnect"); 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) { if (uri) {
initialize(this.state.uri); initialize(getURI(this.state.uri, this.state.password));
} }
} }
connect() { connect() {
if (this.state.autoconnect) { if (this.state.autoconnect) {
localStorage.setItem("autoconnect", this.state.uri); 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() { render() {
@ -48,7 +58,7 @@ class ConnectionOverlay extends Component {
</div> </div>
); );
} }
const { uri, autoconnect } = this.state; const { uri, password, autoconnect } = this.state;
return ( return (
<div className="connection-overlay"> <div className="connection-overlay">
<Card> <Card>
@ -60,9 +70,20 @@ class ConnectionOverlay extends Component {
<Input <Input
id="socket-uri" id="socket-uri"
value={uri} value={uri}
onKeyPress={e => e.keyCode === 13 && this.connect()}
onChange={e => this.setState({ uri: e.target.value })} onChange={e => this.setState({ uri: e.target.value })}
/> />
</FormGroup> </FormGroup>
<FormGroup>
<Label for="socket-password">Server Password</Label>
<Input
id="socket-password"
value={password}
type="password"
onKeyPress={e => e.keyCode === 13 && this.connect()}
onChange={e => this.setState({ password: e.target.value })}
/>
</FormGroup>
<FormGroup> <FormGroup>
<Label for="autoconnect" check> <Label for="autoconnect" check>
<Input <Input
@ -76,7 +97,7 @@ class ConnectionOverlay extends Component {
</FormGroup> </FormGroup>
<button <button
className="btn btn-primary" className="btn btn-primary"
onClick={this.connect.bind(this)} onClick={this.connect}
>{socket.reason ? "Reconnect" : "Connect"}</button> >{socket.reason ? "Reconnect" : "Connect"}</button>
</CardBlock> </CardBlock>
</Card> </Card>

View File

@ -19,6 +19,7 @@ function Server({ server }) {
className="btn btn-sm btn-outline-danger pull-right" className="btn btn-sm btn-outline-danger pull-right"
onClick={() => { onClick={() => {
localStorage.removeItem("autoconnect"); localStorage.removeItem("autoconnect");
localStorage.removeItem("password");
ws_disconnect(); ws_disconnect();
}} }}
>Disconnect</button> >Disconnect</button>