Skip to content

Commit f63e6b0

Browse files
authored
Fix: 401 전역 처리에서 user 쿼리 제외 (#187)
* fix: 사용자와 상호작용할 때만 쿼리 실행하도록 수정 * refactor: 401 전역처리에서 user 쿼리 제외 * feat: 캡슐 상세 헤더에 홈버튼 추가 * refactor: 코드리뷰 반영
1 parent 3326532 commit f63e6b0

7 files changed

Lines changed: 36 additions & 6 deletions

File tree

app/(main)/setting/page.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"use client";
2+
import { HTTPError } from "ky";
3+
import { HTTP_STATUS_CODE } from "@/shared/constants/api";
24
import { useLogout } from "@/app/(auth)/_api/auth.queries";
35
import { userQueryOptions } from "@/shared/api/queries/user";
46
import PopupReport from "@/shared/ui/popup/popup-report";
@@ -10,13 +12,20 @@ import SettingItem from "./_components/setting-item";
1012
import SettingSection from "./_components/setting-section";
1113
import UserGreetingSection from "./_components/user-greeting-section";
1214
import { useOverlay } from "@/shared/hooks/use-overlay";
15+
import { PATH } from "@/shared/constants/path";
1316
import * as styles from "./page.css";
1417

1518
const Setting = () => {
1619
const { mutate: logout } = useLogout();
1720
const router = useRouter();
1821
const queryClient = useQueryClient();
19-
const { data: userInfo } = useQuery(userQueryOptions.userInfo({}));
22+
const { data: userInfo } = useQuery(userQueryOptions.userInfo({
23+
onError: (error) => {
24+
if (error instanceof HTTPError && error.response.status === HTTP_STATUS_CODE.UNAUTHORIZED) {
25+
router.replace(PATH.LOGIN);
26+
}
27+
},
28+
}));
2029
const { open } = useOverlay();
2130

2231
const handleLogout = () => {

app/(sub)/capsule-detail/[invite-code]/[id]/page.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ const CapsuleDetailPage = () => {
3737
const { data, isLoading, isError } = useQuery(
3838
capsuleQueryOptions.capsuleDetail(id),
3939
);
40-
const { data: user } = useQuery(userQueryOptions.userInfo());
40+
const { data: user } = useQuery(
41+
userQueryOptions.userInfo()
42+
);
4143
const { mutate: leaveCapsule } = useLeaveCapsule();
4244
const router = useRouter();
4345
const pathname = usePathname();

shared/api/api-client.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ky, { type Options as KyOptions, type ResponsePromise } from "ky";
22
import { HTTP_STATUS_CODE } from "@/shared/constants/api";
33
import { HTTPError } from "ky";
44
import { PATH } from "@/shared/constants/path";
5+
import { ENDPOINTS } from "@/shared/constants/endpoints";
56

67
const API_BASE_URL =
78
process.env.NODE_ENV === "development"
@@ -21,6 +22,10 @@ const http = ky.create({
2122
async (error) => {
2223
if (error instanceof HTTPError) {
2324
const { response } = error;
25+
const pathname = new URL(error.request.url).pathname;
26+
if (pathname.endsWith(ENDPOINTS.USER_INFO)) {
27+
return error;
28+
}
2429
switch (response.status) {
2530
case HTTP_STATUS_CODE.UNAUTHORIZED: {
2631
window.location.replace(PATH.LOGIN);

shared/api/queries/user.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ const userQueryKeys = {
99
} as const;
1010

1111
export const userQueryOptions = {
12-
userInfo: (options?: { enabled?: boolean }) =>
12+
userInfo: (options?: {
13+
enabled?: boolean;
14+
onError?: (error: Error) => void;
15+
}) =>
1316
queryOptions({
1417
queryKey: userQueryKeys.userInfo(),
1518
queryFn: () => {

shared/assets/icon/home.svg

Lines changed: 1 addition & 1 deletion
Loading

shared/ui/navbar/navbar-detail/index.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import BackIcon from "@/shared/assets/icon/left.svg";
4+
import HomeIcon from "@/shared/assets/icon/home.svg";
45
import { cn } from "@/shared/utils/cn";
56
import { useRouter } from "next/navigation";
67
import type { ReactElement } from "react";
@@ -24,15 +25,24 @@ const NavbarDetail = ({ renderRight, className }: NavbarDetailProps) => {
2425
}
2526
};
2627

28+
const handleHome = () => {
29+
router.push(PATH.HOME);
30+
};
31+
2732
return (
2833
<div className={styles.container}>
34+
<div>
2935
<button
3036
type="button"
3137
onClick={handleBack}
32-
className={styles.backButton}
38+
className={styles.iconButton}
3339
>
3440
<BackIcon />
3541
</button>
42+
<button type="button" onClick={handleHome} className={styles.iconButton}>
43+
<HomeIcon />
44+
</button>
45+
</div>
3646
<div className={cn(styles.rightElement, className)}>
3747
{renderRightElement}
3848
</div>

shared/ui/navbar/navbar-detail/navbar-detail.css.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ export const container = style({
1515
maxWidth: "80rem",
1616
});
1717

18-
export const backButton = style({
18+
export const iconButton = style({
1919
width: "4.4rem",
2020
height: "4.4rem",
21+
marginRight: "1rem",
2122
color: themeVars.color.white[40],
2223
});
2324

0 commit comments

Comments
 (0)