Skip to content

Commit

Permalink
refactor: token check 로직 전체 라우트 적용 및 불필요 코드 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
ydj515 committed Oct 10, 2024
1 parent 426559b commit 7745df1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
27 changes: 11 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,17 @@ import CheckLocalStorage from "./routes/CheckLocalStorage";
const App = () => {
return (
<Router>
<Routes>
<Route
path="/"
element={
<CheckLocalStorage>
<Home />
</CheckLocalStorage>
}
>
<Route index element={<News />} />
<Route path="category/:code" element={<News />} />
<Route path="news/:id" element={<NewsDetail />} />
<Route path="login" element={<Login />} />
<Route path="signup" element={<Signup />} />
</Route>
</Routes>
<CheckLocalStorage>
<Routes>
<Route path="/" element={<Home />}>
<Route index element={<News />} />
<Route path="category/:code" element={<News />} />
<Route path="news/:id" element={<NewsDetail />} />
<Route path="login" element={<Login />} />
<Route path="signup" element={<Signup />} />
</Route>
</Routes>
</CheckLocalStorage>
</Router>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function Login(props) {
}

const { accessToken } = response.data.data;
setTokenWithExpiry("accessToken", accessToken, 24 * 60 * 60 * 1000);
setTokenWithExpiry("accessToken", accessToken, 5 * 60 * 60 * 1000);
Cookies.set("userId", response.data.data.userId, { expires: 1 });
alert("로그인 성공");
window.location.href = "/";
Expand Down
9 changes: 5 additions & 4 deletions src/routes/CheckLocalStorage.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Navigate } from "react-router-dom";
// import { Navigate } from "react-router-dom";
import { getTokenWithExpiry } from "../utils/auth";

const CheckLocalStorage = ({ children }) => {
const token = getTokenWithExpiry("accessToken");

// if (!token) {
// return <Navigate to="/login" />;
// }
if (!token) {
console.log("expired");
// return <Navigate to="/login" />;
}

return children;
};
Expand Down

0 comments on commit 7745df1

Please sign in to comment.