Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
students
messenger
Commits
7566e570
Commit
7566e570
authored
Jan 13, 2021
by
우지원
Browse files
Nav에 user정보
parent
f0e5bcdb
Changes
6
Show whitespace changes
Inline
Side-by-side
client/src/Components/Menu.js
View file @
7566e570
// 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)
//
cons
t userId = localStorage.getItem('user'
)
//
cons
ole.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
>
...
...
client/src/index.js
View file @
7566e570
...
@@ -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
>
...
...
client/src/utils/auth.js
View file @
7566e570
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
'
)
...
...
package.json
View file @
7566e570
...
@@ -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"
:
{
...
...
server/controllers/user.controller.js
View file @
7566e570
...
@@ -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
server/routes/user.routes.js
View file @
7566e570
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment