-
Notifications
You must be signed in to change notification settings - Fork 0
[Fix] axios 인터셉터 status 처리, redirect 로직 수정 #181
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,3 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { notFound, redirect } from 'next/navigation'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import axios from 'axios'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { CommonErrorResponse, CommonSuccessResponse } from '@/types/service/common'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -37,16 +35,6 @@ baseAPI.interceptors.response.use( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return response; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async (error) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const status = error.response?.status; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (status) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (status === 401) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| redirect('/signin?error=unauthorized'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (status === 404) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| notFound(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const errorResponse: CommonErrorResponse = error.response?.data || { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: 'about:blank', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| title: 'Network Error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -56,6 +44,23 @@ baseAPI.interceptors.response.use( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorCode: 'NETWORK_ERROR', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { status } = errorResponse; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isServer = typeof window === 'undefined'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (status === 401) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isServer) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { redirect } = await import('next/navigation'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| redirect('/login'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const currentPath = window.location.pathname + window.location.search; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| window.location.href = `/login?error=unauthorized&path=${encodeURIComponent(currentPath)}`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
50
to
61
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 401 클라이언트 리다이렉트는 } else {
+ if (window.location.pathname === '/login') {
+ throw errorResponse;
+ }
const currentPath = window.location.pathname + window.location.search;
window.location.href = `/login?error=unauthorized&path=${encodeURIComponent(currentPath)}`;
}
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (status === 404) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { notFound } = await import('next/navigation'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| notFound(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
50
to
68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🌐 Web query:
💡 Result: Short answer: notFound() is a server-only API — it is designed to be called from Server Components, route handlers, or Server Functions (files/functions marked to run on the server). Calling notFound() from a Client Component will not work and will produce errors; instead you must either (a) perform the check on the server and call notFound() there, or (b) call a server action (a function marked "use server") which calls notFound(), or (c) handle navigation on the client (e.g. router.replace to a 404 route or your custom not-found UI). [1][2][3] Pointers and sources:
If you want, tell me which component/file you’re trying to call notFound() from and I’ll give a concrete refactor (server function pattern or client navigation) with code. 🌐 Web query:
💡 Result: Short answer: No — notFound() is a server-side App‑Router function and cannot be directly invoked inside a Client Component. Use a server-side route/handler or a Server Action to call notFound(), or navigate the client to a 404 route (e.g. router.push('/404')) from the client instead. [1][2] References
Sources
🌐 Web query:
💡 Result: Likely cause
How to fix (short)
If you want, paste the minimal file(s) (page/component + any imports) and I’ll point exactly which file should be server vs client and what to change. Sources
References 클라이언트에서 현재 코드(59-62줄)는 클라이언트에서도 - if (status === 404) {
- const { notFound } = await import('next/navigation');
- notFound();
- }
+ if (status === 404) {
+ if (isServer) {
+ const { notFound } = await import('next/navigation');
+ notFound();
+ } else {
+ window.location.href = '/404';
+ }
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw errorResponse; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTTP status 분기 기준이 payload(
errorResponse.status)라 오동작 여지 있음Axios의 실제 상태코드(
error.response?.status)를 1순위로 쓰고, payload status는 fallback으로 두는 편이 안전합니다.📝 Committable suggestion
🤖 Prompt for AI Agents