Skip to content

Commit

Permalink
♻️ Refactor Token expiration auto logout
Browse files Browse the repository at this point in the history
#305 token 만료시 자동로그아웃
  • Loading branch information
Herrypi committed Nov 8, 2024
1 parent 963a3ee commit 521f184
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
7 changes: 6 additions & 1 deletion gongjakso/src/pages/ProfilePage/ProfilePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const MAX_PORTFOLIOS = 3;

const ProfilePage = () => {
const [data, setProfileData] = useState();
const authenticated = localStorage.getItem('accessToken');
const [isLoggedIn, setIsLoggedIn] = useState(!!authenticated);
const [postContent1, setPostContent1] = useState();
const [postContent2, setPostContent2] = useState();
const [postContent3, setPostContent3] = useState();
Expand All @@ -37,6 +39,9 @@ const ProfilePage = () => {
const [portfolioList, setPortfolioList] = useState([]);

useEffect(() => {
if (!isLoggedIn) {
navigate(`/`);
}
getMyInfo().then(response => {
setProfileData(response?.data);
});
Expand All @@ -49,7 +54,7 @@ const ProfilePage = () => {
getMyParticipated(1, 2).then(response => {
setPostContent3(response?.data?.content);
});
}, []);
}, [isLoggedIn]);
useEffect(() => {
if (selectedPortfolioId && selectedPortfolioType) {
fetchPortfolioDetailsByType(
Expand Down
12 changes: 9 additions & 3 deletions gongjakso/src/service/portfolio_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export const getAllPortfolio = async () => {
const response = await axiosInstanceV2.get(reqURL);
return response;
} catch (error) {
console.log(error);
error.response.data.code = 3004
? localStorage.removeItem('accessToken')
: console.log(error.response.data.message);
return error.response.data.code;
}
};
Expand Down Expand Up @@ -49,7 +51,9 @@ export const getPortfolio = async id => {
const response = await axiosInstanceV2.get(reqURL);
return response;
} catch (error) {
console.log(error);
error.response.data.code = 3004
? localStorage.removeItem('accessToken')
: console.log(error.response.data.message);
return error.response?.data.code;
}
};
Expand All @@ -62,7 +66,9 @@ export const getExistPortfolio = async (id, type) => {
const response = await axiosInstanceV2.get(reqURL);
return response;
} catch (error) {
console.log('Error getting portfolio details:', error);
error.response.data.code = 3004
? localStorage.removeItem('accessToken')
: console.log(error.response.data.message);
return error.response.data.code;
}
};
Expand Down
4 changes: 3 additions & 1 deletion gongjakso/src/service/post_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ export const getMyPortfolio = async () => {
const response = await axiosInstanceV2.get(reqURL);
return response.data;
} catch (error) {
console.log(error);
error.response.data.code = 3004
? localStorage.removeItem('accessToken')
: console.log(error.response.data.message);
}
};

Expand Down
29 changes: 22 additions & 7 deletions gongjakso/src/service/profile_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const putMyInfo = async (name, major, job, status, phone) => {
});
return response.data;
} catch (error) {
console.log(error);
throw new Error('나의 정보를 가져올 수 없습니다.');
}
};
Expand All @@ -25,7 +26,9 @@ export const getMyInfo = async () => {
const response = await axiosInstance.get(reqURL);
return response.data;
} catch (error) {
console.log('내가 지원한 게시글을 가져올 수 없습니다.');
error.response.data.code = 3004
? localStorage.removeItem('accessToken')
: console.log(error.response.data.message);
}
};

Expand All @@ -37,7 +40,9 @@ export const getMyRecruiting = async (page, size) => {
const response = await axiosInstanceV2.get(reqURL);
return response.data;
} catch (error) {
console.log('내가 모집 중인 게시글을 가져올 수 없습니다.', error);
error.response.data.code = 3004
? localStorage.removeItem('accessToken')
: console.log(error.response.data.message);
}
};

Expand All @@ -49,7 +54,9 @@ export const getMyApplied = async page => {
const response = await axiosInstanceV2.get(reqURL);
return response.data;
} catch (error) {
console.log('내가 지원한 게시글을 가져올 수 없습니다.');
error.response.data.code = 3004
? localStorage.removeItem('accessToken')
: console.log(error.response.data.message);
}
};

Expand All @@ -62,7 +69,9 @@ export const getMyParticipated = async (page, size) => {
const response = await axiosInstanceV2.get(reqURL);
return response.data;
} catch (error) {
console.log(error);
error.response.data.code = 3004
? localStorage.removeItem('accessToken')
: console.log(error.response.data.message);
}
};

Expand All @@ -72,7 +81,9 @@ export const getMyContestScrap = async () => {
const response = await axiosInstance.get(reqURL);
return response.data;
} catch (error) {
console.log('내가 스크랩한 공모전 정보를 가져올 수 없습니다.');
error.response.data.code = 3004
? localStorage.removeItem('accessToken')
: console.log(error.response.data.message);
}
};

Expand All @@ -82,7 +93,9 @@ export const getMyProjectScrap = async () => {
const response = await axiosInstance.get(reqURL);
return response.data;
} catch (error) {
console.log('내가 스크랩한 프로젝트 정보를 가져올 수 없습니다.');
error.response.data.code = 3004
? localStorage.removeItem('accessToken')
: console.log(error.response.data.message);
}
};
export const getMyTeamScrap = async (page, size) => {
Expand All @@ -91,6 +104,8 @@ export const getMyTeamScrap = async (page, size) => {
const response = await axiosInstanceV2.get(reqURL);
return response.data;
} catch (error) {
console.log('내가 스크랩한 프로젝트 정보를 가져올 수 없습니다.');
error.response.data.code = 3004
? localStorage.removeItem('accessToken')
: console.log(error.response.data.message);
}
};

0 comments on commit 521f184

Please sign in to comment.