Skip to content

Commit

Permalink
fix: api 호출 axios로 전환
Browse files Browse the repository at this point in the history
  • Loading branch information
haejinyun committed Dec 9, 2024
1 parent 92b0971 commit 0672035
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 40 deletions.
7 changes: 7 additions & 0 deletions src/apis/queryFunctions/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
PostRegisterResponseData,
PostEmailAuthParams,
CheckEmailAuthParams,
LoginParams,
PostLoginResponseData,
} from '@/apis/types/Auth';
import { Response } from '@/apis/types/common';

Expand All @@ -29,3 +31,8 @@ export const postEmailValidationCode = async (params: CheckEmailAuthParams) => {
const response = await apiClient.post<Response<boolean>>('v2/mail/code', params);
return response.data;
};

export const postLogin = async (params: LoginParams) => {
const response = await apiClient.post<Response<PostLoginResponseData>>('v2/auth/login', params);
return response.data;
};
10 changes: 8 additions & 2 deletions src/apis/queryFunctions/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function createApiClient() {
},
async error => {
const refreshToken = localStorage.getItem('refreshToken');

const originalRequest = error.config;

if (error.response?.status === 401) {
Expand All @@ -60,8 +59,15 @@ function createApiClient() {
originalRequest.headers.Authorization = newAccessToken;
return await client(originalRequest);
}

console.error('refreshToken도 만료되었습니다.');
alert('재로그인이 필요합니다.');
localStorage.removeItem('refreshToken');
sessionStorage.removeItem('accessToken');
window.location.href = '/login';
} catch (e) {
console.error('Failed to reissue token:', e);
alert('재로그인이 필요합니다.');
console.error('accessToken 재발급 실패');
localStorage.removeItem('refreshToken');
sessionStorage.removeItem('accessToken');
window.location.href = '/login';
Expand Down
34 changes: 0 additions & 34 deletions src/apis/queryFunctions/setTokenCookies.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/apis/queryHooks/Auth/useLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMutation } from '@tanstack/react-query';
import { AxiosError } from 'axios';
import { useRouter } from 'next/navigation';

import setTokenCookies from '@/apis/queryFunctions/setTokenCookies';
import { postLogin } from '@/apis/queryFunctions/Auth';
import { LoginParams } from '@/apis/types/Auth';
import { Response } from '@/apis/types/common';
import { useAuth } from '@/provider/authProvider';
Expand All @@ -17,10 +17,10 @@ function useLogin() {
const { setIsLoggedIn } = useAuth();

const { mutate } = useMutation({
mutationFn: (loginParams: LoginParams) => setTokenCookies(loginParams),
mutationFn: (loginParams: LoginParams) => postLogin(loginParams),
onSuccess: data => {
sessionStorage.setItem('accessToken', data.accessToken);
localStorage.setItem('refreshToken', data.refreshToken);
sessionStorage.setItem('accessToken', data.result_data.access_token);
localStorage.setItem('refreshToken', data.result_data.refresh_token);
setIsLoggedIn(true);
showToast('success', '로그인 되었습니다.');

Expand Down

0 comments on commit 0672035

Please sign in to comment.