Menu.js 1.52 KB
Newer Older
우지원's avatar
우지원 committed
1
import axios from 'axios';
우지원's avatar
0113    
우지원 committed
2
import React, { useState, useEffect } from 'react';
Choi Ga Young's avatar
Choi Ga Young committed
3
import { Navbar, Nav, Button } from 'react-bootstrap';
우지원's avatar
우지원 committed
4
import { handleLogout, isAuthenticated } from '../utils/auth';
Choi Ga Young's avatar
Choi Ga Young committed
5
6

function Menu() {
우지원's avatar
우지원 committed
7
  const user = isAuthenticated()
우지원's avatar
우지원 committed
8
  const [userName, setUsername] = useState('')
우지원's avatar
우지원 committed
9
10
11
12
13

  async function getLoginedUser() {
    const userId = localStorage.getItem('user')
    const response = await axios.get(`/users/${userId}`)
    console.log(response.data.username)
우지원's avatar
우지원 committed
14

우지원's avatar
우지원 committed
15
    const resName = response.data.username
우지원's avatar
우지원 committed
16
17
    //const resName = JSON.stringify(response.data.username)
    console.log(resName)
우지원's avatar
우지원 committed
18
19
20
21
22
23
24
25
26
    setUsername(resName)
  }
  console.log(userName)

  useEffect((user) => {
    if (localStorage.getItem('user')) {
      getLoginedUser(user)
    }
  }, [])
우지원's avatar
0113    
우지원 committed
27

Choi Ga Young's avatar
Choi Ga Young committed
28
29
30
31

  return (
    <Navbar bg="dark" variant="dark">
      <Navbar.Brand href="/home">YDK Messenger</Navbar.Brand>
우지원's avatar
우지원 committed
32
33
34

      {user ?
        <>
우지원's avatar
우지원 committed
35
          <div className='ml-1 mr-2' style={{ color: 'white' }}>{userName}  환영합니다</div>
우지원's avatar
우지원 committed
36
37
38
39
40
41
42
43
44
45
46
47
          <Nav className="mr-auto">
            <Nav.Link href="/home">Home</Nav.Link>
            <Nav.Link href="/profile">Profile</Nav.Link>
            <Nav.Link href="/hello">Hello</Nav.Link>
          </Nav>
          <Button className="ml-auto" onClick={() => handleLogout()} variant="light" className="ml-3">Logout</Button>
        </>
        : <Nav className="ml-auto">
          <Nav.Link href='/login'>로그인</Nav.Link>
          <Nav.Link href='/signup'>회원가입</Nav.Link>
        </Nav>}

Choi Ga Young's avatar
Choi Ga Young committed
48
49
50
51
52
    </Navbar>
  )
}

export default Menu