-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
98 lines (96 loc) · 2.9 KB
/
index.html
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
<html>
<head>
<script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js" integrity="sha512-bZS47S7sPOxkjU/4Bt0zrhEtWx0y0CRkhEp8IckzK+ltifIIE9EMIMTuT/mEzoIMewUINruDBIR/jJnbguonqQ==" crossorigin="anonymous"></script>
</head>
<body>
<div id='root'></div>
<script type='text/babel'>
class SignIn extends React.Component{
constructor(){
super();
this.state = {
username: '',
password: ''
};
this.onChange = this.onChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
}
onChange(ev){
this.setState({ [ev.target.name]: ev.target.value });
}
onSubmit(ev){
ev.preventDefault();
const { username, password } = this.state;
this.props.signIn({
username,
password
});
}
render(){
const { onChange, onSubmit } = this;
const { username, password } = this.state;
return (
<form onSubmit={ onSubmit }>
<input value={ username } onChange={ onChange } name='username'/>
<input value={ password } onChange={ onChange } name='password'/>
<button>Sign In</button>
</form>
);
}
}
class App extends React.Component{
constructor(){
super();
this.state = {
auth: {}
};
this.signIn = this.signIn.bind(this);
this.logout = this.logout.bind(this);
}
logout(){
window.localStorage.removeItem('token');
this.setState({ auth: {}});
}
async attemptTokenLogin(){
const token = window.localStorage.getItem('token');
if(token){
const response = await axios.get('/api/auth', {
headers: {
authorization: token
}
});
this.setState({ auth: response.data });
}
}
componentDidMount(){
this.attemptTokenLogin();
}
async signIn(credentials){
let response = await axios.post('/api/auth', credentials);
const { token } = response.data;
window.localStorage.setItem('token', token);
this.attemptTokenLogin();
}
render(){
const { auth } = this.state;
const { signIn, logout } = this;
if(!auth.id){
return <SignIn signIn={ signIn }/>
}
else {
return (
<div>
Welcome { auth.username }
<button onClick={ logout }>Logout</button>
</div>
);
}
}
}
ReactDOM.render(<App />, document.querySelector('#root'));
</script>
</body>
</html>