-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLogin.jsx
More file actions
executable file
·182 lines (166 loc) · 6.22 KB
/
Login.jsx
File metadata and controls
executable file
·182 lines (166 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import React, { Component } from 'react';
import { Redirect } from 'react-router-dom';
import Grid from '@material-ui/core/Grid';
import { withStyles } from '@material-ui/core/styles';
import Typography from '@material-ui/core/Typography';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Checkbox from "@material-ui/core/Checkbox";
import Paper from '@material-ui/core/Paper';
const styles = {
root: {
padding: '20px',
textAlign: "center"
},
buttonDivCenter: {
display: 'flex',
alignItems: 'center',
justifyContent: 'center'
},
};
class Login extends Component {
constructor(){
super();
this.state = {
username : null,
password : null,
auth_token : null,
remember: false,
redirect : false,
error_message : ''
}
this.handleUserChange = this.handleUserChange.bind(this);
this.handlePassChange = this.handlePassChange.bind(this);
this.handleLogin = this.handleLogin.bind(this);
};
handleUserChange(event){
this.setState({username: event.target.value});
};
handlePassChange(event){
this.setState({password: event.target.value});
};
handleLogin(){
// console.log("user: "+this.state.username+" password: "+this.state.password);
event.preventDefault();
let data = {username:this.state.username, password:this.state.password};
// console.log(data)
let method = 'POST';
let options = { method: method,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)};
fetch('/api/auth/login', options)
.then(results => {
return results.json();
}).then(response => {
console.log(response);
if (response['status'] == 'success'){
this.setState({auth_token:response['auth_token']});
sessionStorage.setItem('username', this.state.username);
sessionStorage.setItem('auth_token', this.state.auth_token);
if (this.state.remember) {
localStorage.setItem('username', this.state.username);
localStorage.setItem('auth_token', this.state.auth_token);
}
localStorage.setItem('user_id', response['user_id']);
this.setState({redirect:true});
}
else {
console.log("failed to log in");
// TODO: add an invalid username or password update
this.setState({error_message:response['message']});
console.log(this.state.error_message)
}
});
}
handleChecked(event) {
this.setState({remember: event.target.checked});
}
handleRedirect(){
if (this.state.redirect){
return (<Redirect to='/'/>);
}
}
handleError(){
if (!(this.state.error_message === '')){
console.log('error')
return (
<div>
<Typography style={{color: 'red'}}>
{this.state.error_message}
</Typography>
</div>
);
}
}
handleKeyPress(event){
if (event.key === 'Enter'){
this.handleLogin();
}
}
render(){
const { classes } = this.props;
return (
<Paper className="grid-containers login-container">
<div>
<div className={classes.root}>
<Typography variant="h4" style={{color: '#28324C'}}>
Login
</Typography>
</div>
<div>
{this.handleError()}
</div>
<div>
<TextField
style={{width: "100%"}}
required
id="outlined-required"
label="Username"
name="username"
margin="normal"
variant="outlined"
onChange={(event) => this.handleUserChange(event)}
error={!(this.state.error_message === '')}
/>
</div>
<div>
<TextField
style={{width: "100%"}}
required
id="outlined-password-input"
label="Password"
type="password"
name="password"
margin="normal"
variant="outlined"
onChange={(event) => this.handlePassChange(event)}
error={!(this.state.error_message === '')}
onKeyDown={ (event) => this.handleKeyPress(event)}
/>
</div>
<div>
<FormControlLabel label="Remember Me"
control={<Checkbox checked={this.state.remember} onChange={(event) => this.handleChecked(event)} />} />
</div>
<div>
{this.handleRedirect()}
</div>
<div className={classes.buttonDivCenter}>
<Button
style={{color: "#118851", marginTop: "10px"}}
onClick={ () => this.handleLogin()}
variant="contained"
name="submit"
>
Submit
</Button>
</div>
</div>
</Paper>
)
}
}
export default withStyles(styles)(Login);