Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[S1] 로그인 관련 예외 처리 #616

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import MagazinePage from './page/MagazinePage';
import CartPage from './page/CartPage';
import ExpirationPage from './page/Expiration/ExpirationPage';
import OrderDepositPage from './page/Order/OrderDepositPage';
import Interceptors from './libs/hooks/Interceptors';

const Router = () => {
return (
<BrowserRouter>
<Interceptors />
<ScrollToTop />
<Routes>
<Route path='/list' element={<ListPage />} />
Expand Down
10 changes: 6 additions & 4 deletions src/common/SideMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ const SideMenu = ({ isSideMenuOpen, setIsSideMenuOpen }: SideMenuProps) => {
};

const handleClickMyTattooButton = () => {
navigate('/my-tattoo');
isLogin ? navigate('/my-tattoo') : navigate('/login');
};

const handleClickCartButton = () => {
isLogin ? navigate('/cart') : navigate('/login');
};

const NAV_MENU_ITEM = [
Expand All @@ -47,9 +51,7 @@ const SideMenu = ({ isSideMenuOpen, setIsSideMenuOpen }: SideMenuProps) => {
{
icon: <IcCart />,
text: '장바구니',
clickHandler: () => {
navigate('/cart');
},
clickHandler: handleClickCartButton,
},
];

Expand Down
23 changes: 10 additions & 13 deletions src/components/Detail/DetailFooter.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import styled from 'styled-components';
import { IcHeartDark, IcHeartLight } from '../../assets/icon';
import { useNavigate } from 'react-router-dom';
import { api } from '../../libs/api';
import { api, getAccessToken } from '../../libs/api';
import { useState } from 'react';
import Toast from '../../common/ToastMessage/Toast';

Expand All @@ -28,12 +28,16 @@ const DetailFooter = ({
const navigate = useNavigate();
const [toast, setToast] = useState(false);

const handleClickButton = () => {
if (like === null) {
const checkLogin = () => {
if (!getAccessToken()) {
// 로그인 상태가 아닌 경우
setToast(true);
return;
return false;
}
};

const handleClickButton = () => {
if (!checkLogin()) return;
if (isSecond) {
// state로 OrderPage에서 필요한 정보 넘겨주기
navigate(`/order`, {
Expand Down Expand Up @@ -90,10 +94,8 @@ const DetailFooter = ({
};

const handleClickLike = () => {
if (like === null) {
// 로그인 상태가 아닌 경우
setToast(true);
} else if (like) {
if (!checkLogin()) return;
if (like) {
// 로그인 상태인 경우
// 만약 원래 좋아요가 눌린 상태면
postDisliked();
Expand All @@ -105,11 +107,6 @@ const DetailFooter = ({
};

const handleCartButton = () => {
if (like === null) {
// 로그인 상태가 아닌 경우
setToast(true);
return;
}
postCart();
};

Expand Down
26 changes: 26 additions & 0 deletions src/libs/hooks/Interceptors.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { api, removeAccessToken } from '../api';

const Interceptors = () => {
const navigate = useNavigate();

useEffect(() => {
api.interceptors.response.use(
(res) => res,
(err) => {
console.log(err);
if (err.response.status === 401) {
removeAccessToken();
navigate('/expired');
return new Promise(() => null);
}
return Promise.reject(err);
},
);
}, []);

return null;
};

export default Interceptors;