Skip to content

Commit f10d606

Browse files
authored
Merge pull request #85 from Team-INSERT/fix/token
액세스 토큰 재발급 오류 재수정
2 parents cb5fa47 + 552511b commit f10d606

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

src/apis/error/throwAxiosError.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { isAxiosError } from "axios";
2-
import { toast } from "react-toastify";
32

43
const throwAxiosError = (err: unknown) => {
54
if (!isAxiosError(err))
@@ -12,7 +11,6 @@ const throwAxiosError = (err: unknown) => {
1211
const data = err?.response?.data;
1312
const { code, status, message } = data;
1413
console.log(err);
15-
toast.error("오류가 발생했습니다.");
1614

1715
return { code, status, message };
1816
};

src/apis/httpClient/httpClient.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
22
import { requestInterceptors, responseInterceptors } from "@/apis/interceptor";
3-
import { KEY, TOKEN } from "@/constants/";
4-
import { QueryClient } from "@tanstack/react-query";
3+
import { ERROR, TOKEN } from "@/constants/";
54
import Storage from "../storage";
5+
import { refresh } from "../token";
66

77
export interface HttpClientConfig {
88
baseURL?: string;
@@ -132,16 +132,15 @@ export class HttpClient {
132132

133133
private setting() {
134134
HttpClient.setCommonInterceptors(this.api);
135-
const queryClient = new QueryClient();
136135

137136
this.api.interceptors.response.use(
138137
(response) => response,
139-
(error) => {
140-
queryClient.invalidateQueries([
141-
KEY.USER,
142-
Storage.getItem(TOKEN.ACCESS),
143-
]);
144-
return Promise.reject(error);
138+
async (error) => {
139+
const originalRequest = error.config;
140+
if (error.response.data.code === ERROR.CODE.TOKEN_403_2) {
141+
await refresh();
142+
return this.api(originalRequest);
143+
}
145144
},
146145
);
147146
}
@@ -164,7 +163,6 @@ export default {
164163
post: new HttpClient("api/post/", axiosConfig),
165164
recomment: new HttpClient("api/recomment", axiosConfig),
166165
comment: new HttpClient("api/comment", axiosConfig),
167-
refresh: new HttpClient("api/auth/refresh/access", axiosConfig),
168166
auth: new HttpClient("api/auth/", axiosConfig),
169167
bamboo: new HttpClient("api/bamboo", axiosConfig),
170168
admin: new HttpClient("api/bamboo/admin", axiosConfig),

src/apis/token/refresh.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { TOKEN } from "@/constants/";
22
import Storage from "@/apis/storage";
3-
import httpClient from "../httpClient";
3+
import axios from "axios";
4+
5+
const instance = axios.create({
6+
baseURL: process.env.NEXT_PUBLIC_BASE_URL,
7+
});
48

59
const refresh = async () => {
610
try {
7-
const { data } = await httpClient.refresh.put({
11+
const { data } = await instance.put("/api/auth/refresh/access", {
812
refreshToken: `${Storage.getItem(TOKEN.REFRESH)}`,
913
});
1014
Storage.setItem(TOKEN.ACCESS, data.accessToken);

src/hooks/useUser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const useUser = (options?: UseUserOptions) => {
3030
error,
3131
refetch,
3232
} = useQuery<IUser>(
33-
[KEY.USER, Storage.getItem(TOKEN.ACCESS)],
33+
[KEY.USER],
3434
async () => {
3535
const { data } = await httpClient.user.get(authorization());
3636
return data;

0 commit comments

Comments
 (0)