|
1 | | -import { clientAxios } from "./clientAxios"; |
2 | | -import { serverAxios } from "./serverAxios"; |
| 1 | +import axios, { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from "axios"; |
3 | 2 |
|
4 | | -const isServer = typeof window === "undefined"; |
| 3 | +import { EXTERNAL_PATH } from "@/shared/constants/path"; |
| 4 | +import { IS_LOGINED } from "@/shared/constants/storage"; |
| 5 | +import { isAxiosErrorResponse } from "@/shared/types/axioxError"; |
5 | 6 |
|
6 | | -export const instance = isServer ? serverAxios : clientAxios; |
| 7 | +import { safeSessionStorage } from "../storage"; |
| 8 | +import { refreshAccessToken } from "./helpers"; |
| 9 | +import { ERROR_CODE } from "./utils/errorCode"; |
| 10 | + |
| 11 | +export const instance = axios.create({ |
| 12 | + baseURL: process.env.NEXT_PUBLIC_TICKET_API_BASE_URL, |
| 13 | + timeout: 10_000, |
| 14 | + headers: { |
| 15 | + "Content-Type": "application/json", |
| 16 | + }, |
| 17 | + withCredentials: true, |
| 18 | +}); |
| 19 | + |
| 20 | +// 요청 인터셉터 |
| 21 | +instance.interceptors.request.use( |
| 22 | + (config: InternalAxiosRequestConfig) => { |
| 23 | + return config; |
| 24 | + }, |
| 25 | + (error: AxiosError) => { |
| 26 | + return Promise.reject(error); |
| 27 | + }, |
| 28 | +); |
| 29 | + |
| 30 | +let tokenRefreshPromise: Promise<void> | null = null; |
| 31 | +let isLoginAlertShown = false; |
| 32 | + |
| 33 | +// 응답 인터셉터 |
| 34 | +instance.interceptors.response.use( |
| 35 | + (response: AxiosResponse) => response?.data, |
| 36 | + async (error: AxiosError) => { |
| 37 | + if (typeof window === "undefined") { |
| 38 | + // Server에서는 기본 전파 |
| 39 | + console.error(error); |
| 40 | + |
| 41 | + return Promise.reject(error); |
| 42 | + } |
| 43 | + |
| 44 | + console.error("##error", JSON.stringify(error)); |
| 45 | + |
| 46 | + if (isAxiosErrorResponse(error.response?.data)) { |
| 47 | + // 엑세스 토큰 없음 |
| 48 | + if ( |
| 49 | + error.response?.data.code === ERROR_CODE.NO_ACCESS_TOKEN || |
| 50 | + error.response?.data.code === ERROR_CODE.REFRESH_TOKEN_EXPIRED |
| 51 | + ) { |
| 52 | + redirectToLoginOnce(); |
| 53 | + } |
| 54 | + |
| 55 | + if (error?.response?.data.code === ERROR_CODE.NO_ACCESS) { |
| 56 | + alert("접근 권한이 필요한 페이지입니다."); |
| 57 | + |
| 58 | + window.location.href = EXTERNAL_PATH.HOME; |
| 59 | + } |
| 60 | + |
| 61 | + // 액세스 토큰 만료 |
| 62 | + if (error.response?.data.code === ERROR_CODE.ACCESS_TOKEN_EXPIRED) { |
| 63 | + try { |
| 64 | + // 이미 토큰 재발급이 진행 중이면 완료될 때까지 대기 |
| 65 | + if (tokenRefreshPromise) { |
| 66 | + await tokenRefreshPromise; |
| 67 | + } else { |
| 68 | + // 토큰 재발급 시작 |
| 69 | + tokenRefreshPromise = refreshAccessToken() |
| 70 | + .then(() => { |
| 71 | + tokenRefreshPromise = null; |
| 72 | + }) |
| 73 | + .catch((error) => { |
| 74 | + tokenRefreshPromise = null; |
| 75 | + |
| 76 | + throw error; |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + // 원래 요청 재시도 |
| 81 | + const originalRequest = error.config; |
| 82 | + |
| 83 | + if (!originalRequest) { |
| 84 | + return Promise.reject(error); |
| 85 | + } |
| 86 | + |
| 87 | + return instance(originalRequest); |
| 88 | + } catch { |
| 89 | + if (typeof window !== "undefined") { |
| 90 | + redirectToLoginOnce(); |
| 91 | + |
| 92 | + return Promise.reject(error?.response?.data); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (error.response?.status === 500) { |
| 99 | + return Promise.reject(error?.response?.data); |
| 100 | + } |
| 101 | + |
| 102 | + return Promise.reject(error?.response?.data); |
| 103 | + }, |
| 104 | +); |
| 105 | + |
| 106 | +function redirectToLoginOnce() { |
| 107 | + if (isLoginAlertShown) return; |
| 108 | + |
| 109 | + isLoginAlertShown = true; |
| 110 | + |
| 111 | + alert("로그인이 필요한 페이지입니다."); |
| 112 | + safeSessionStorage.remove(IS_LOGINED); |
| 113 | + |
| 114 | + const redirectUrl = `${EXTERNAL_PATH.LOGIN}?redirectUrl=${encodeURIComponent( |
| 115 | + window.location.origin + window.location.pathname + window.location.search, |
| 116 | + )}`; |
| 117 | + |
| 118 | + window.location.href = redirectUrl; |
| 119 | +} |
0 commit comments