박민정

[fix] Fix logout button error

1 import React, {useEffect} from 'react' 1 import React, {useEffect} from 'react'
2 import axios from 'axios' 2 import axios from 'axios'
3 +import { withRouter } from 'react-router-dom';
3 4
4 -function LandingPage() { 5 +function LandingPage(props) {
6 +
7 + // 로그아웃 버튼 클릭 됐을 때
8 + const onLogoutClickedEvent = () => {
9 + axios.get('/api/users/logout')
10 + .then(response => {
11 + // 만약 success:true이면 로그인 페이지로 가기
12 + if(response.data.success)
13 + props.history.push("/login");
14 + else
15 + alert("Fail to logout.")
16 + })
17 + }
5 // 랜딩페이지에 들어오자마자 18 // 랜딩페이지에 들어오자마자
6 useEffect(() => { 19 useEffect(() => {
7 axios.get('/api/hello') // get request를 서버로 보냄 (endpoint는 /api/hello) 20 axios.get('/api/hello') // get request를 서버로 보냄 (endpoint는 /api/hello)
...@@ -11,11 +24,12 @@ function LandingPage() { ...@@ -11,11 +24,12 @@ function LandingPage() {
11 return ( 24 return (
12 <div style={{justifyContent:'center', alignItems: 'center', display:'flex', width:'100%'}}> 25 <div style={{justifyContent:'center', alignItems: 'center', display:'flex', width:'100%'}}>
13 <h1>시작 페이지</h1> 26 <h1>시작 페이지</h1>
14 - <button> Logout </button> 27 + <button onClick ={onLogoutClickedEvent}> Logout </button>
15 </div> 28 </div>
16 29
17 30
18 ) 31 )
19 } 32 }
20 33
21 -export default LandingPage 34 +
35 +export default withRouter(LandingPage)
...\ No newline at end of file ...\ No newline at end of file
......