Skip to content

Commit

Permalink
Merge pull request #282 from hhbb0081/test
Browse files Browse the repository at this point in the history
Feat: 토큰 만료 시 로그인 페이지로 이동
  • Loading branch information
hhbb0081 authored Mar 26, 2024
2 parents b2c2d8c + 1bbb36e commit 1497431
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 147 deletions.
9 changes: 4 additions & 5 deletions src/hoc/auth.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { message } from "antd";
import moment from "moment";
import React, { useEffect } from "react";
import { useCookies } from "react-cookie";
import { useLocation, useNavigate } from "react-router-dom";
import { useRecoilState } from "recoil";
import { isAuthenticatedState } from "../Atom/status";
import authApi from "../utils/authApi";
import commonApis from "../utils/commonApis";

function Auth(SpecificComponent, option, adminRoute = null) {
function AuthenticationCheck(props) {
Expand All @@ -17,7 +16,7 @@ function Auth(SpecificComponent, option, adminRoute = null) {

useEffect(() => {
function fetchAuth() {
authApi.get("/auth", {
commonApis.get("/auth", {
withCredentials: true,
headers: {
Authorization: `Bearer ${token ? token : cookies?.accessToken}`
Expand All @@ -34,15 +33,15 @@ function Auth(SpecificComponent, option, adminRoute = null) {
// 로그인 후 첫 접속
console.log('auth에서 첫 접속: ', cookies.accessToken);
localStorage.setItem("accessToken", cookies.accessToken); // 로컬 스토리지에 AT 저장
localStorage.setItem("expiredTime", moment().add(1, "days").format("yyyy-MM-DD HH:mm:ss")); // 만료시간 저장
// localStorage.setItem("expiredTime", moment().add(1, "days").format("yyyy-MM-DD HH:mm:ss")); // 만료시간 저장
setIsAuth(true); // 로그인 여부 변경
message.success("로그인에 성공하셨습니다.");
removeCookie("accessToken"); // AT 쿠키 삭제
return;
} else if(token) {
localStorage.clear();
localStorage.setItem("accessToken", cookies.accessToken); // 로컬 스토리지에 AT 저장
localStorage.setItem("expiredTime", moment().add(1, "days").format("yyyy-MM-DD HH:mm:ss")); // 만료시간 저장
// localStorage.setItem("expiredTime", moment().add(1, "days").format("yyyy-MM-DD HH:mm:ss")); // 만료시간 저장
setIsAuth(true); // 로그인 여부 변경
removeCookie("accessToken"); // AT 쿠키 삭제
return;
Expand Down
96 changes: 0 additions & 96 deletions src/utils/authApi.jsx

This file was deleted.

63 changes: 17 additions & 46 deletions src/utils/commonApis.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,22 @@ commonApis.interceptors.request.use(
}
);

// commonApis.interceptors.response.use(
// (res) => { return res },
// async (err) => {
// const {
// config,
// response,
// } = err;
// console.log(err);
// // access token 만료 시

// const loginStateValue = localStorage.getItem('accessToken');
// // console.log('commonApi: ', loginStateValue);
// // console.log(response);

// // if(!loginStateValue){
// // // 로그인 안 되어 있는 경우
// // setIsAuth(true);
// // }
// try{
// if (response?.status === 403 && loginStateValue) {
// console.log("로그인 O, 403");
// const res = await axios.get(process.env.REACT_APP_API_ROOT + "/api/v1/refresh/token", {
// withCredentials: true,
// headers: {
// Authorization: `Bearer ${loginStateValue}`
// }
// })
// console.log(res);
// localStorage.clear();
// // removeCookie("accessToken");
// // axios.defaults.headers.common.Authorization = "Bearer " + accessToken;
// // config.headers.common["Authorization"] = "Bearer " + accessToken;
// return axios(config);
// }
// } catch (e) {
// if (e?.response?.status === 401 || e?.response?.status === 403){
// // 로그인 페이지로 이동
// console.log(e);
// localStorage.removeItem("accessToken");
// window.location.href = "/login";
// }
// return config;
// }
// return Promise.reject(err);
// }
// )
commonApis.interceptors.response.use(
(res) => { return res },
(err) => {
const {
config,
response,
} = err;
console.log(err);
// access token 만료 시
if (response?.status && response?.status === 403) {
window.location.href = "/login";
localStorage.clear();
return config;
}
return Promise.reject(err);
}
)

export default commonApis;

0 comments on commit 1497431

Please sign in to comment.