Skip to content

Commit

Permalink
Merge pull request #103 from CoolPeace-yanolza/feature/#102
Browse files Browse the repository at this point in the history
[#102] 로그아웃 API 연결
  • Loading branch information
JitHoon authored Jan 24, 2024
2 parents 6178511 + afd49b3 commit d356c15
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { default as instance } from './lib/instance';
export { default as postLogin } from './lib/postLogin';
export { default as postLogout } from './lib/postLogout';
export { default as postRefreshToken } from './lib/postRefreshToken';
export {
getCouponList,
Expand Down
4 changes: 4 additions & 0 deletions src/api/lib/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ const onErrorResponse = async (error: AxiosResponseError<string>) => {
// 리프레시 토큰도 만료되었을 때 = 재로그인 안내
} else if (tokenResponse.status === 404) {
console.log(tokenResponse.data.message);
// await postLogout();
// deleteAllCookies();
// navigate('/login');

return Promise.reject(error);
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/api/lib/postLogout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// import { AxiosError } from 'axios';

import { instance } from '..';

const postLogout = async () => {
await instance.post('/v1/member/logout');

/* HACK: 로그아웃 에러 response 가 있을 경우 사용
try {
const response = await instance.post('/v1/member/logouts');
return response;
} catch (error) {
if (error instanceof AxiosError) error.response;
}
*/
};

export default postLogout;
74 changes: 66 additions & 8 deletions src/components/common/Layout/Header/User/UserModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,79 @@
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import styled from '@emotion/styled';

import { ErrorModal } from '@components/common';
import { postLogout } from 'src/api';
import { getCookies, deleteAllCookies } from '@utils/lib/cookies';
import { UserModal, UserModalStyleProps } from '@/types/layout';
import { getCookies } from '@utils/lib/cookies';
import ERROR_MODAL_MESSAGE from 'src/constants/lib/ERROR_MODAL_MESSAGE';
import theme from '@styles/theme';

const UserModal = ({ isOpen }: UserModal) => {
const [isErrorModalOpen, setIsErrorModalOpen] = useState(false);
const navigate = useNavigate();

const userName = getCookies('userName');
const userEmail = getCookies('userEmail');

const ButtonFunc = () => setIsErrorModalOpen(false);

const handleLogout = async () => {
await postLogout();
deleteAllCookies();
navigate('/login');
/* HACK: 로그아웃 에러 response 가 있을 경우 사용
[ 대안 1 ]
const response = await postLogout();
if (response === undefined) {
setIsErrorModalOpen(true);
} else if (response?.status === 200) {
deleteAllCookies();
navigate('/login');
}
[ 대안 2 ]
try {
const response = await postLogout();
deleteAllCookies();
navigate('/login');
}catch (error) {
const modalContent = {
text: '로그아웃에 실패하였습니다.',
errorText: '잠시후 재시도 해주세요.'
};
const ButtonFunc = () => {};
ErrorModal({ modalContent, ButtonFunc });
}
*/
};

return (
<Modal $isOpen={isOpen}>
<UserInformation>
<Name $isOpen={isOpen}>{userName}</Name>
<Email $isOpen={isOpen}>{userEmail}</Email>
</UserInformation>
<Logout $isOpen={isOpen}>로그아웃</Logout>
</Modal>
<>
<Modal $isOpen={isOpen}>
<UserInformation>
<Name $isOpen={isOpen}>{userName}</Name>
<Email $isOpen={isOpen}>{userEmail}</Email>
</UserInformation>
<Logout
$isOpen={isOpen}
onClick={handleLogout}
>
로그아웃
</Logout>
</Modal>
{isErrorModalOpen && (
<ErrorModal
modalContent={ERROR_MODAL_MESSAGE.USER_MODAL}
ButtonFunc={ButtonFunc}
/>
)}
</>
);
};

Expand Down
12 changes: 12 additions & 0 deletions src/constants/lib/ERROR_MODAL_MESSAGE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const ERROR_MODAL_MESSAGE = {
LOGIN: {
text: '이메일 또는 비밀번호를 잘못 입력했습니다.',
errorText: '입력하신 내용을 다시 확인해주세요.'
},
USER_MODAL: {
text: '로그아웃에 실패하였습니다.',
errorText: '잠시후 재시도 해주세요.'
}
};

export default ERROR_MODAL_MESSAGE;
7 changes: 2 additions & 5 deletions src/pages/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import styled from '@emotion/styled';

import { ErrorModal, Footer } from '@components/common';
import { LoginForm, LoginTitle } from '@components/Login';
import ERROR_MODAL_MESSAGE from 'src/constants/lib/ERROR_MODAL_MESSAGE';

const Login = () => {
const initialValue = {
text: '이메일 또는 비밀번호를 잘못 입력했습니다.',
errorText: '입력하신 내용을 다시 확인해주세요.'
};
const [modalContent, setModalContent] = useState(initialValue);
const [modalContent, setModalContent] = useState(ERROR_MODAL_MESSAGE.LOGIN);
const [isModalOpen, setIsModalOpen] = useState(false);

const handleModalClose = () => setIsModalOpen(prev => !prev);
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export {
export { getUpdatedDate } from './lib/calculation';
export { getStatusToLocaleString } from './lib/dashboard';
export { getInputOptions } from './lib/auth';
export { setCookies, getCookies, deleteAllCookies } from './lib/cookies';
7 changes: 7 additions & 0 deletions src/utils/lib/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ export const getCookies: GetCookies = name => {
?.split('=')[1];
return cookieValue;
};

export const deleteAllCookies = () => {
document.cookie.split(';').forEach(cookie => {
const [name] = cookie.trim().split('=');
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;`;
});
};

0 comments on commit d356c15

Please sign in to comment.