Skip to content

Commit

Permalink
chore : 리프레쉬토큰 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
hyuna committed Jul 14, 2024
1 parent 87fefb5 commit 9996a77
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
39 changes: 14 additions & 25 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import axios, { AxiosError } from "axios";
import { cookie } from "@/utils/auth";
import { useRouter } from "next/navigation";

const BASEURL = process.env.NEXT_PUBLIC_API_KEY;

Expand Down Expand Up @@ -38,40 +37,30 @@ refreshInstance.interceptors.request.use(

instance.interceptors.response.use(
(response) => response,
async (error: AxiosError<AxiosError>) => {
async (error: AxiosError) => {
if (axios.isAxiosError(error) && error.response) {
const { status } = error.response.data;
const { status } = error.response;
if (status === 401) {
const refreshToken = cookie.get("refresh_token");
if (refreshToken) {
try {
const res = await axios.put(
`${BASEURL}/refresh`,
{},
{
await axios
.put(`${BASEURL}/admin/refresh`, null, {
headers: {
"X-Refresh-Token": `Bearer ${refreshToken}`,
"X-Refresh-Token": `${refreshToken}`,
},
}
);
const { data } = res.data;
const accessToken = data.accessToken;
cookie.set("access_token", accessToken);
if (error.config) {
error.config.headers.Authorization = `Bearer ${accessToken}`;
return axios.request(error.config);
}
} catch {
throw error;
})
.then((response) => {
const data = response.data;
cookie.set("access_token", data.access_token);
cookie.set("refresh_token", data.refresh_token);
});
} catch (refreshError) {
return Promise.reject(refreshError);
}
} else {
throw error;
}
} else {
throw error;
}
} else {
throw error;
}
return Promise.reject(error);
}
);
2 changes: 1 addition & 1 deletion src/app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const Login: NextPage = () => {

const onClickBtn = async () => {
try {
const result = await loginMutate(data, {
await loginMutate(data, {
onSuccess: (res) => {
const accessToken = res.access_token;
const refreshToken = res.refresh_token;
Expand Down
4 changes: 1 addition & 3 deletions src/hook/apiError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ const ApiError = () => {
alert("400 잘못된 요청입니다");
};

const handle401: ErrorHandler = () => {
router.push("login");
};
const handle401: ErrorHandler = () => {};

const handle403: ErrorHandler = () => {
alert("403 권한이 없습니다");
Expand Down

0 comments on commit 9996a77

Please sign in to comment.