MypickPage.js
1.54 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
import React, { useState, useEffect } from 'react';
import NavBar from '../components/NavBar';
import LoginLink from '../components/LoginLink';
import UserCards from '../components/UserCards';
import { Container } from 'reactstrap';
const MypickPage = () => {
const [isLogin, setIsLogin] = useState(false);
const [userName, setUserName] = useState('');
const authApi = () => {
const user = JSON.parse(localStorage.getItem('user'));
return fetch('/api/auth', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'authorization': user
}
}).then(response => response.json())
.then(result => {
if(result.message === 'valid token') {
setIsLogin(true);
setUserName(result.username);
} else if(result.message === 'expired token') {
// alert('토큰이 만료되었습니다. 로그인 해주세요.');
setIsLogin(false);
} else if(result.message === 'invalid token') {
setIsLogin(false);
}
});
}
useEffect(() => {
authApi();
}, [isLogin]);
return (
<>
<Container>
<NavBar/>
{
isLogin ?
(<>
<UserCards username={userName} isLogin={setIsLogin}/>
</>)
:
(<>
<LoginLink/>
</>)
}
</Container>
</>
)
}
export default MypickPage;