Commit 9ad05544 authored by Kim, Subin's avatar Kim, Subin
Browse files

context 사용 PrivateRouter 적용

parent 8165ef72
...@@ -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} />;
......
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 (
......
...@@ -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) {
......
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