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
Today KU
Commits
9ad05544
Commit
9ad05544
authored
Nov 04, 2021
by
Kim, Subin
Browse files
context 사용 PrivateRouter 적용
parent
8165ef72
Changes
3
Hide whitespace changes
Inline
Side-by-side
client/src/components/PrivateRoute.js
View file @
9ad05544
...
...
@@ -8,7 +8,7 @@ const PrivateRoute = ({ component: Component, ...rest }) => {
<
Route
{...
rest
}
render
=
{(
props
)
=>
{
if
(
user
)
{
if
(
user
.
id
!==
""
)
{
if
(
rest
.
role
)
{
if
(
rest
.
role
===
user
.
role
)
{
return
<
Component
{...
props
}
/>
;
...
...
client/src/utils/context.js
View file @
9ad05544
import
{
createContext
,
useCallback
,
useContext
,
useEffect
,
useState
}
from
'
react
'
;
import
{
useHistory
,
useLocation
}
from
"
react-router-dom
"
;
import
ErrorPage
from
"
../pages/ErrorPage
"
;
import
authApi
from
"
../apis/auth.api
"
;
import
catchErrors
from
'
./catchErrors
'
;
...
...
@@ -19,10 +20,14 @@ const AuthProvider = ({ children }) => {
const
{
pathname
}
=
useLocation
()
const
getUser
=
async
()
=>
{
try
{
console
.
log
(
"
context getUser
"
)
const
resUser
=
await
authApi
.
getUser
();
setUser
({
...
user
,
...
resUser
});
// console.log("context use")
// if (pathname === "/admin" && user.role !== "admin") history.push('/home')
setUser
({
...
user
,
...
resUser
})
if
(
pathname
===
"
/admin
"
&&
user
.
role
!==
"
admin
"
)
{
await
logout
()
return
<
ErrorPage
/>
}
else
if
(
user
.
role
===
"
admin
"
)
history
.
push
(
"
/admin
"
)
else
history
.
push
(
"
/home
"
)
}
catch
(
error
)
{
catchErrorAuth
(
error
,
setError
);
}
...
...
@@ -37,8 +42,7 @@ const AuthProvider = ({ children }) => {
setError
(
""
);
const
user
=
await
authApi
.
login
(
data
);
localStorage
.
setItem
(
"
login
"
,
true
)
setUser
(
user
);
console
.
log
(
'
setUser 결과
'
,
user
)
setUser
(
user
)
return
true
;
}
catch
(
error
)
{
catchErrors
(
error
,
setError
);
...
...
@@ -80,6 +84,7 @@ const AuthProvider = ({ children }) => {
console
.
log
(
"
Error message:
"
,
errMsg
)
}
displayError
(
errMsg
);
alert
(
errMsg
)
},
[]);
return
(
...
...
server/controllers/user.controller.js
View file @
9ad05544
...
...
@@ -9,7 +9,6 @@ const getUser = async (req, res) => {
const
{
id
,
role
,
name
}
=
jwt
.
verify
(
token
,
config
.
jwtSecret
);
return
res
.
json
({
id
,
role
,
name
});
}
else
{
// res.json({ id: "", role: "user", name: "" });
throw
new
Error
(
"
유효기간이 만료되었습니다. 다시 로그인해주세요.
"
)
}
}
catch
(
error
)
{
...
...
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