/
index.js
46 lines (40 loc) · 978 Bytes
/
index.js
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
import React from 'react';
import Link from 'next/link.js';
import axios from 'axios';
import { Cookies } from 'react-cookie';
const serverUrl = 'http://localhost:3001';
// set up cookies
const cookies = new Cookies();
class Index extends React.Component {
constructor(props) {
super(props);
this.state = {
token: cookies.get('token') || null
}
}
onLoginClick = async () => {
console.log("Login called");
const response = await axios.get(serverUrl + '/api/login')
const token = response.data.token;
cookies.set('token', token);
this.setState({
token: token
})
}
render() {
return (
<div>
<h2>Main page</h2>
<br></br>
<button onClick={() => this.onLoginClick()}>Get Token</button>
<br></br>
<p>Token: {this.state.token}</p>
<br></br>
<Link href="/secret">
<a>Secret page</a>
</Link>
</div >
)
}
}
export default Index;