-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.ts
More file actions
139 lines (121 loc) · 3.88 KB
/
index.ts
File metadata and controls
139 lines (121 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import axios, { AxiosError } from 'axios';
import { tokenStorage } from '@/components/auth/util/accessToken';
import { ADMIN_API_KEY, ADMIN_API_URL, API_URL, AUTH_API_URL, CREW_API_URL, OPERATION_API_URL } from '@/constants/env';
export const axiosInstance = axios.create({
baseURL: API_URL,
headers: {
'Accept': '*/*',
'Content-Type': 'application/json',
},
});
export const axiosAuthInstance = axios.create({
baseURL: AUTH_API_URL,
headers: {
'Accept': '*/*',
'Content-Type': 'application/json',
},
withCredentials: true,
});
export const axiosAdminInstance = axios.create({
baseURL: ADMIN_API_URL,
headers: {
'Accept': '*/*',
'Content-Type': 'application/json',
'api-key': ADMIN_API_KEY,
},
});
export const axiosCrewInstance = axios.create({
baseURL: CREW_API_URL,
headers: {
'Accept': '*/*',
'Content-Type': 'application/json',
},
});
// 어드민 운영 프로덕트 API 연결을 위한 axiosInstance
export const axiosOperationInstance = axios.create({
baseURL: OPERATION_API_URL,
headers: {
'Accept': '*/*',
'Content-Type': 'application/json',
'api-key': ADMIN_API_KEY,
},
});
axiosInstance.interceptors.request.use(
(config) => {
const token = tokenStorage.get();
if (token && config.headers && config.headers.Authorization === undefined) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => Promise.reject(error),
);
axiosInstance.interceptors.response.use(
(res) => res,
async (err) => handleTokenError(err),
);
axiosCrewInstance.interceptors.request.use(
(config) => {
const token = tokenStorage.get();
if (token && config.headers && config.headers.Authorization === undefined) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => Promise.reject(error),
);
axiosCrewInstance.interceptors.response.use(
(res) => res,
async (err) => handleTokenError(err),
);
export const handleTokenError = async (error: AxiosError<unknown>) => {
const originRequest = error.config;
if (!error.response || !originRequest) throw new Error('에러가 발생했습니다.');
const { status } = error.response;
if (status === 401) {
if (window.location.pathname === '/makers') {
// 메이커스 페이지는 로그인 필요 없음
return Promise.reject(error);
}
if (window.location.pathname === '/policy/person') {
// 개인정보 처리방침 페이지는 로그인 필요 없음, 구글 정책상 오픈
return Promise.reject(error);
}
// 앱 딥링크를 위한 .well-known 경로는 인증 체크 제외
if (window.location.pathname.startsWith('/.well-known')) {
return Promise.reject(error);
}
/** 토큰이 없으면 refresh 시도하지 않고 바로 intro로 이동 */
const currentToken = tokenStorage.get();
if (currentToken === null && window.location.pathname !== '/intro') {
window.location.replace('/intro');
throw new Error('토큰이 없습니다.');
}
try {
const { data } = await axiosAuthInstance.post<{ data: { accessToken: string } }>(
`/api/v1/auth/refresh/web`,
null,
{
headers: {
Authorization: `Bearer ${tokenStorage.get()}`,
},
},
);
if (!originRequest.headers) {
originRequest.headers = {};
}
originRequest.headers.Authorization = `Bearer ${data.data.accessToken}`;
tokenStorage.set(data.data.accessToken);
return axiosInstance(originRequest);
} catch (error) {
console.error(error);
tokenStorage.remove();
/** intro 페이지에서 토큰 무효 시 무한 재로드를 막기 위함 */
if (window.location.pathname !== '/intro') {
window.location.replace('/intro');
}
throw new Error('토큰 갱신에 실패하였습니다.');
}
}
return Promise.reject(error);
};