Commit 7566e570 authored by 우지원's avatar 우지원
Browse files

Nav에 user정보

parent f0e5bcdb
// import axios from 'axios'; import axios from 'axios';
import { response } from 'express';
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { Navbar, Nav, Button } from 'react-bootstrap'; import { Navbar, Nav, Button } from 'react-bootstrap';
import { handleLogout, isAuthenticated } from '../utils/auth'; import { handleLogout, isAuthenticated } from '../utils/auth';
// const INIT_USER = { const INIT_USER = {
// username: '' username: ''
// } }
function Menu() { function Menu() {
const userName = "정연우";
const user = isAuthenticated() const user = isAuthenticated()
// const [userName, setUsername] = useState(INIT_USER) const [userName, setUsername] = useState(INIT_USER)
async function getLoginedUser() {
const userId = localStorage.getItem('user')
const response = await axios.get(`/users/${userId}`)
console.log(response.data.username)
const resName = response.data.username
// async function getLoginedUser() { // const resName = JSON.stringify(response.data.username)
// const userId = localStorage.getItem('user') // console.log(resName)
// const response = await axios.get(`/users/${userId}`) setUsername(resName)
// setUsername(response.data) }
// } console.log(userName)
// useEffect((user) => { useEffect((user) => {
// if (localStorage.getItem('user')) { if (localStorage.getItem('user')) {
// getLoginedUser(user) getLoginedUser(user)
// } }
// }, []) }, [])
return ( return (
...@@ -33,7 +37,7 @@ function Menu() { ...@@ -33,7 +37,7 @@ function Menu() {
{user ? {user ?
<> <>
<div className='ml-1 mr-2' style={{ color: 'white' }}>{response.data} 환영합니다</div> <div className='ml-1 mr-2' style={{ color: 'white' }}>{userName} 환영합니다</div>
<Nav className="mr-auto"> <Nav className="mr-auto">
<Nav.Link href="/home">Home</Nav.Link> <Nav.Link href="/home">Home</Nav.Link>
<Nav.Link href="/profile">Profile</Nav.Link> <Nav.Link href="/profile">Profile</Nav.Link>
......
...@@ -12,7 +12,6 @@ import { BrowserRouter as Router, Route, Redirect, Switch } from 'react-router-d ...@@ -12,7 +12,6 @@ import { BrowserRouter as Router, Route, Redirect, Switch } from 'react-router-d
import Hello from './Hello' import Hello from './Hello'
import HomePage from './Pages/HomePage' import HomePage from './Pages/HomePage'
ReactDOM.render( ReactDOM.render(
<Router> <Router>
<Switch> <Switch>
......
import axios from "axios" import axios from "axios"
//자동으로 localstorage에 login이 생성됨 //자동으로 localstorage에 login이 생성됨
export function handleLogin(props) { export function handleLogin(userId) {
localStorage.setItem('user', props.email) localStorage.setItem('user', userId)
} }
export async function handleLogout() { export async function handleLogout() {
localStorage.removeItem('user') localStorage.removeItem('user')
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
"main": "index.js", "main": "index.js",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "nodemon server/server.js", "dev": "nodemon --ignore client server/server.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": { "repository": {
......
...@@ -89,6 +89,34 @@ const changenick = async (req, res) => { ...@@ -89,6 +89,34 @@ const changenick = async (req, res) => {
} }
} }
const loginNavbar = async (req, res) => {
try {
let user = await User.findOne(req.body).select('username').exec()
return res.json(user)
} catch (error) {
alert('올바르지 못한 접근입니다.')
}
}
// const getNav = (req, res) => {
// res.json(req.loginNav)
// }
// const userById = async (req, res, next, id) => {
// try {
// const user = await User.findById(id)
// if (!user) {
// res.status(404).send('사용자를 찾을 수 없습니다.')
// }
// req.loginNav = user
// //req에 profile을 만들어서 user를 저장해줌
// next()
// //다음 middleware로 이동시킴 / 없으면 멈춤
// } catch (error) {
// console.log(error)
// res.status(500).send('사용자 아이디 검색 실패')
// }
// }
export default { signup, hello, logineduser, changenick } export default { signup, hello, logineduser, changenick, loginNavbar }
// {} : 객체로 return함 // {} : 객체로 return함
\ No newline at end of file
...@@ -11,8 +11,9 @@ router.route('/users/signup') ...@@ -11,8 +11,9 @@ router.route('/users/signup')
router.route(`/users/:userId`) router.route(`/users/:userId`)
.post(userCtrl.logineduser) .post(userCtrl.logineduser)
.put(userCtrl.changenick) .put(userCtrl.changenick)
.get(userCtrl.loginNavbar)
// router.param('userId', userCtrl.userById)
// router.route('/api/users/signup/:userId') //로그인한 사람의 정보만 가져오도록 // router.route('/api/users/signup/:userId') //로그인한 사람의 정보만 가져오도록
// .get // .get
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment