Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fe/#248] token reissuance #255

Open
wants to merge 4 commits into
base: fe/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions fe/src/api/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CreateServiceApi = (service: string): AxiosInstance => {
const portMap: ServiceEndpoints = {
review: 8082,
member: 8081,
notifictation: 8084,
notification: 8084,
// ๋‹ค๋ฅธ ์„œ๋น„์Šค๋“ค์˜ ํฌํŠธ ๋ฒˆํ˜ธ ์ถ”๊ฐ€
};

Expand All @@ -28,9 +28,49 @@ const CreateServiceApi = (service: string): AxiosInstance => {
// Interceptor๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ accessToken์„ ์š”์ฒญ์— ์ถ”๊ฐ€
// const { authToken } = useAuthStore();
// const accessToken = localStorage.getItem('accessToken');
api.interceptors.response.use(
(response) => {
return response;
},
async (error) => {
const {
config,
response: { status },
} = error;

const originalRequest = config;

if (status === 403) {
const accessToken = localStorage.getItem('access');
const refreshToken = localStorage.getItem('refresh');

try {
const { data } = await axios({
method: 'post',
url: '/members/token',
data: { accessToken, refreshToken },
});
const newAccessToken = data.data.accessToken;
const newRefreshToken = data.data.refreshToken;
originalRequest.headers = {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + newAccessToken,
};
localStorage.setItem('access', newAccessToken);
localStorage.setItem('refresh', newRefreshToken);
return await axios(originalRequest);
} catch (error) {
console.error(error);
// locate ์‚ฌ์šฉํ•ด๋ณด๊ธฐ (์ด๊ฒƒ๋„ ๊ด€์ !) window.locate.์„ ์ด์šฉํ•ด์„œ ํ™ˆํ™”๋ฉด์œผ๋กœ ์—๋Ÿฌ์‹œ ๋ณด๋‚ด๋ฒ„๋ ค๋ผ!
// ํ† ํฐ ๋ฐœ๊ธ‰๋œ๊ฑฐ ์‚ญ์ œํ•˜๊ธฐ -> ๋กœ์ปฌ์— ๋“ค์–ด๊ฐ„๊ฑฐ ๋”ฐ๋ผ์„œ ์ €์žฅํ•ด๋†จ์œผ๋‹ˆ๊นŒ, ๋นผ๋†“๋Š”๊ฒƒ๋„ ์ถ”๊ฐ€๋ฅผ ํ•ด์•ผ ์ˆœ์ˆ˜ํ•œ ํ•จ์ˆ˜์ด๊ณ , ์—๋Ÿฌ์ฒ˜๋ฆฌ๊ฐ€ ๋œ๋‹ค!
}
}
},
);

api.interceptors.request.use((config) => {
config.headers.Authorization = 'Bearer accessToken';
const accessToken = localStorage.getItem('access');
config.headers.Authorization = `Bearer ${accessToken}`;
return config;
});
return api;
Expand Down
22 changes: 14 additions & 8 deletions fe/src/api/memberApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios';
import axios, { isAxiosError } from 'axios';
import clientApi from './axios';
import { v4 as uuidv4 } from 'uuid';

Expand All @@ -17,13 +17,19 @@ const memberApi = {
//!์•„๋ž˜ API๊นŒ์ง€๋Š” accessToken - null์ธ ์ƒํƒœ๋กœ ๋‚ ์•„๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!
//ํšŒ์›๊ฐ€์ž… [post]
signUp: async ({ certification, password }: userProps) => {
return await axios.post(
`${baseURL}/api/v1/members/`,
{ certification, password },
{
headers: headers,
},
);
return await axios
.post(
`${baseURL}/api/v1/members/`,
{ certification, password },
{
headers: headers,
},
)
.catch((e) => {
if (isAxiosError(e)) {
e.status;
}
});
},
//๋กœ๊ทธ์ธ [post]
login: async ({ certification, password }: userProps) => {
Expand Down
65 changes: 45 additions & 20 deletions fe/src/hooks/query/member/useRefreshToken.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
import memberApi from '@api/memberApi';
import { useMutation } from '@tanstack/react-query';
// import memberApi from '@api/memberApi';
// import { useMutation } from '@tanstack/react-query';
// import axios from 'axios';

const useRefreshToken = () => {
return useMutation({
mutationFn: memberApi.refreshToken,
onSuccess: (data) => {
console.log('์š”์ฒญ ์„ฑ๊ณต');
console.log(data);
},
onError: (err) => {
console.error('์—๋Ÿฌ ๋ฐœ์ƒ');
console.log(err);
},
onSettled: (data) => {
console.log('๊ฒฐ๊ณผ์— ๊ด€๊ณ„ ์—†์ด ๋ฌด์–ธ๊ฐ€ ์‹คํ–‰๋จ');
console.log(data);
},
});
};
// const useRefreshToken = () => {
// return useMutation({
// mutationFn: memberApi.refreshToken,
// onSuccess: async (data) => {
// console.log('์š”์ฒญ ์„ฑ๊ณต');
// console.log(data);

export default useRefreshToken;
// const tokenData = data?.data?.data;

// const access = tokenData.accessToken;
// const refresh = tokenData.refreshToken;
// console.log(access, refresh);
// if (access && refresh) {
// localStorage.setItem('access', access);

// const lastApiEndpoint = localStorage.getItem('lastApiEndpoint');

// if (lastApiEndpoint !== null) {
// const response = await axios.get(lastApiEndpoint, {
// headers: {
// Authorization: `Bearer ${access}`,
// },
// });

// // ์ดํ›„ response๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋‚˜ ํ•„์š”์— ๋”ฐ๋ผ ์ƒํƒœ ์—…๋ฐ์ดํŠธ ๋“ฑ์„ ์ˆ˜ํ–‰
// console.log('API ๋‹ค์‹œ ์š”์ฒญ ๊ฒฐ๊ณผ:', response.data);
// } else {
// console.error('๋งˆ์ง€๋ง‰ API ์—”๋“œํฌ์ธํŠธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.');
// }
// }
// },
// onError: (err) => {
// console.error('์—๋Ÿฌ ๋ฐœ์ƒ');
// console.log(err);
// },
// onSettled: (data) => {
// console.log('๊ฒฐ๊ณผ์— ๊ด€๊ณ„ ์—†์ด ๋ฌด์–ธ๊ฐ€ ์‹คํ–‰๋จ');
// console.log(data);
// },
// });
// };

// export default useRefreshToken;