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