Skip to content

Commit e297494

Browse files
committed
Update Login
1 parent 9e0e7b8 commit e297494

22 files changed

+959
-149
lines changed

websocket-react/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"base-64": "^0.1.0",
7+
"material-ui": "^1.0.0-beta.23",
68
"react": "^16.2.0",
79
"react-dom": "^16.2.0",
810
"react-scripts": "1.0.17",
11+
"react-stomp": "^1.0.4",
912
"react-websocket": "^2.0.0",
1013
"sockjs-client": "^1.1.4",
11-
"stompjs": "^2.3.3"
14+
"stompjs": "^2.3.3",
15+
"typeface-roboto": "^0.0.45"
1216
},
1317
"scripts": {
1418
"start": "react-scripts start",
1519
"build": "react-scripts build",
1620
"test": "react-scripts test --env=jsdom",
1721
"eject": "react-scripts eject"
1822
},
19-
"proxy": "http://localhost:8080"
23+
"proxy": "http://localhost:9292"
2024
}

websocket-react/src/App.js

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,15 @@
11
import React, { Component } from 'react';
2+
import 'typeface-roboto'
23
import logo from './logo.svg';
34
import './App.css';
4-
// import EmployeeList from './EmployeeList'
5-
import SockJS from 'sockjs-client'
6-
import Stomp from 'stompjs'
7-
class App extends Component {
5+
import LoginFrom from './LoginForm'
6+
import SockJsClient from 'react-stomp'
87

9-
constructor(props) {
10-
super(props)
11-
let socket = new SockJS("/gs-guide-websocket");
12-
this.stompClient = Stomp.over(socket);
13-
14-
}
158

9+
class App extends Component {
1610

1711

1812
handleClick() {
19-
// Create stomp client over sockJS protocol (see Note 1)
20-
21-
22-
// callback function to be called when stomp client is connected to server (see Note 2)
23-
let connectCallback = function () {
24-
alert("connected!");
25-
this.stompClient.subscribe('/topic/greetings', function (greeting) {
26-
alert(JSON.parse(greeting.body).content);
27-
});
28-
};
29-
30-
// callback function to be called when stomp client could not connect to server (see Note 3)
31-
let errorCallback = function (error) {
32-
// display the error's message header:
33-
alert(error.headers.message);
34-
};
35-
36-
// Connect as guest (Note 4)
37-
this.stompClient.connect("guest", "guest", connectCallback, errorCallback);
38-
39-
// function showGreeting(message) {
40-
// alert(message);
41-
// }
42-
43-
// // Connect as guest (Note 4)
44-
// this.stompClient.connect({}, function (frame) {
45-
// console.log('Connected: ' + frame);
46-
// this.stompClient.subscribe('/topic/greetings', function (greeting) {
47-
// showGreeting(JSON.parse(greeting.body).content);
48-
// });
49-
// });
5013

5114
}
5215

@@ -61,8 +24,11 @@ class App extends Component {
6124
<img src={logo} className="App-logo" alt="logo" />
6225
<h1 className="App-title">Welcome to React</h1>
6326
</header>
64-
<button onClick={this.handleClick.bind(this)}>Connect</button>
65-
<button onClick={this.handleSendName.bind(this)}>Send Name</button>
27+
<LoginFrom />
28+
<div>
29+
<SockJsClient url='http://localhost:9292/ws' topics={['/user/queue/notify']}
30+
onMessage={(msg) => { console.log(msg); }} />
31+
</div>
6632

6733
</div>
6834
);

websocket-react/src/LoginForm.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import React, { Component } from 'react'
2+
import PropTypes from 'prop-types';
3+
import { withStyles } from 'material-ui/styles'
4+
import TextField from 'material-ui/TextField'
5+
import Button from 'material-ui/Button'
6+
import base64 from 'base-64'
7+
8+
9+
10+
const styles = theme => ({
11+
container: {
12+
display: 'flex',
13+
flexWrap: 'wrap',
14+
},
15+
textField: {
16+
marginLeft: theme.spacing.unit,
17+
marginRight: theme.spacing.unit,
18+
width: 200,
19+
},
20+
button: {
21+
margin: theme.spacing.unit * 2,
22+
}
23+
});
24+
25+
class LoginForm extends Component {
26+
state = {
27+
username: '',
28+
password: ''
29+
};
30+
31+
handleLogin(e) {
32+
33+
const { username, password } = this.state
34+
alert(username + password)
35+
36+
let headers = new Headers();
37+
headers.append('Authorization', `Basic ${base64.encode('clientapp:123456')}`);
38+
39+
const body = {
40+
41+
'username': username,
42+
'password': password
43+
}
44+
fetch(`/oauth/token?grant_type=password&username=${username}&password=${password}`, {
45+
method: "post",
46+
headers: headers
47+
})
48+
.then(response => {
49+
return response.json()
50+
})
51+
.then(json => {
52+
localStorage.setItem('result', JSON.stringify(json));
53+
})
54+
.catch(error => { throw error });
55+
}
56+
57+
handleChange = name => event => {
58+
this.setState({
59+
[name]: event.target.value,
60+
});
61+
};
62+
63+
render() {
64+
const { classes } = this.props
65+
return (
66+
<form className={classes.container}>
67+
<TextField
68+
id="username"
69+
label="Username"
70+
className={classes.textField}
71+
value={this.state.name}
72+
onChange={this.handleChange('username')}
73+
margin="normal"
74+
/>
75+
<TextField
76+
id="password"
77+
label="Password"
78+
type="password"
79+
className={classes.textField}
80+
value={this.state.name}
81+
onChange={this.handleChange('password')}
82+
margin="normal"
83+
/>
84+
<Button raised color="primary" className={classes.button} onClick={this.handleLogin.bind(this)} >
85+
Login
86+
</Button>
87+
</form>
88+
)
89+
}
90+
}
91+
92+
LoginForm.propTypes = {
93+
classes: PropTypes.object.isRequired,
94+
}
95+
96+
export default withStyles(styles)(LoginForm)

0 commit comments

Comments
 (0)