Skip to content

Commit e37c063

Browse files
committed
fix(accessToken): remove unnecessary token verification logic
1 parent 85784ed commit e37c063

File tree

7 files changed

+46
-20
lines changed

7 files changed

+46
-20
lines changed

frontend/src/apis/comment.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createAxiosInstance } from '../utils/axiosInstance';
22
import { CommentListResponse, CommentRequest } from '../models/Comment';
33

44
const instanceWithoutToken = createAxiosInstance();
5+
const instanceWithToken = (accessToken) => createAxiosInstance({ accessToken });
56

67
export const getComments = async (studylogId: number): Promise<CommentListResponse> => {
78
const response = await instanceWithoutToken.get(`/studylogs/${studylogId}/comments`);
@@ -12,25 +13,31 @@ export const getComments = async (studylogId: number): Promise<CommentListRespon
1213
export const createCommentRequest = ({
1314
studylogId,
1415
body,
16+
accessToken,
1517
}: {
1618
studylogId: number;
1719
body: CommentRequest;
18-
}) => instanceWithoutToken.post(`/studylogs/${studylogId}/comments`, body);
20+
accessToken: string;
21+
}) => instanceWithToken(accessToken).post(`/studylogs/${studylogId}/comments`, body);
1922

2023
export const editComment = ({
2124
studylogId,
2225
commentId,
2326
body,
27+
accessToken,
2428
}: {
2529
studylogId: number;
2630
commentId: number;
2731
body: CommentRequest;
28-
}) => instanceWithoutToken.put(`/studylogs/${studylogId}/comments/${commentId}`, body);
32+
accessToken: string;
33+
}) => instanceWithToken(accessToken).put(`/studylogs/${studylogId}/comments/${commentId}`, body);
2934

3035
export const deleteComment = ({
3136
studylogId,
3237
commentId,
38+
accessToken,
3339
}: {
3440
studylogId: number;
3541
commentId: number;
36-
}) => instanceWithoutToken.delete(`/studylogs/${studylogId}/comments/${commentId}`);
42+
accessToken: string;
43+
}) => instanceWithToken(accessToken).delete(`/studylogs/${studylogId}/comments/${commentId}`);

frontend/src/contexts/UserProvider.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const UserProvider = ({ children }) => {
5757
client.defaults.headers['Authorization'] = `Bearer ${accessToken}`;
5858
setState((prev) => ({ ...prev, accessToken }));
5959
},
60+
onLogout
6061
});
6162

6263
function onLogout() {

frontend/src/hooks/queries/comment.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useMutation, useQuery, useQueryClient } from 'react-query';
22
import { createCommentRequest, deleteComment, editComment, getComments } from '../../apis/comment';
33
import { CommentRequest } from '../../models/Comment';
4+
import LOCAL_STORAGE_KEY from "../../constants/localStorage";
45

56
const QUERY_KEY = {
67
comments: 'comments',
@@ -12,7 +13,11 @@ export const useFetchComments = (studylogId: number) =>
1213
export const useCreateComment = (studylogId: number) => {
1314
const queryClient = useQueryClient();
1415

15-
return useMutation((body: CommentRequest) => createCommentRequest({ studylogId, body }), {
16+
return useMutation((body: CommentRequest) => createCommentRequest({
17+
studylogId,
18+
body,
19+
accessToken: localStorage.getItem(LOCAL_STORAGE_KEY.ACCESS_TOKEN) as string
20+
}), {
1621
onSuccess() {
1722
queryClient.invalidateQueries([QUERY_KEY.comments, studylogId]);
1823
},
@@ -24,7 +29,7 @@ export const useEditCommentMutation = (studylogId: number) => {
2429

2530
return useMutation(
2631
({ commentId, body }: { commentId: number; body: CommentRequest }) =>
27-
editComment({ studylogId, commentId, body }),
32+
editComment({ studylogId, commentId, body, accessToken: localStorage.getItem(LOCAL_STORAGE_KEY.ACCESS_TOKEN) as string }),
2833
{
2934
onSuccess() {
3035
queryClient.invalidateQueries([QUERY_KEY.comments, studylogId]);
@@ -36,7 +41,7 @@ export const useEditCommentMutation = (studylogId: number) => {
3641
export const useDeleteCommentMutation = (studylogId: number) => {
3742
const queryClient = useQueryClient();
3843

39-
return useMutation((commentId: number) => deleteComment({ studylogId, commentId }), {
44+
return useMutation((commentId: number) => deleteComment({ studylogId, commentId, accessToken: localStorage.getItem(LOCAL_STORAGE_KEY.ACCESS_TOKEN) as string }), {
4045
onSuccess() {
4146
queryClient.invalidateQueries([QUERY_KEY.comments, studylogId]);
4247
},

frontend/src/hooks/queries/studylog.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ export const useGetRecentStudylogsQuery = () => {
3434
const { accessToken } = user;
3535

3636
return useQuery<Studylog[]>([QUERY_KEY.recentStudylogs], async () => {
37-
const response = await requestGetStudylogs({
38-
query: { type: 'searchParams', data: 'size=3' },
39-
accessToken,
40-
});
41-
const { data } = await response.data;
42-
43-
return data;
37+
try {
38+
const response = await requestGetStudylogs({
39+
query: {type: 'searchParams', data: 'size=3'},
40+
accessToken,
41+
});
42+
const {data} = await response.data;
43+
return data;
44+
} catch (error) {
45+
alert(ERROR_MESSAGE.DEFAULT);
46+
}
4447
});
4548
};
4649

frontend/src/hooks/useFilterWithParams.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const useFilterWithParams = () => {
1818
...memberFilter,
1919
]);
2020

21-
const [postQueryParams, setPostQueryParams] = useState({
22-
page: query.page ? query.page : 1,
21+
const [postQueryParams, setPostQueryParams] = useState<{ page?: number }>({
22+
page: query.page ? Number(query.page) : 1,
2323
});
2424

2525
const onSetPage = (page) => {

frontend/src/hooks/useRequest.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ const useRequest = (defaultValue, callback, onSuccess, onError, onFinish) => {
2020
setResponse(responseData);
2121
onSuccess?.(responseData);
2222
} catch (error) {
23+
24+
if (!response.isLoggedIn) {
25+
setError(ERROR_CODE.EXPIRED_ACCESS_TOKEN);
26+
onError?.({
27+
code: ERROR_CODE.EXPIRED_ACCESS_TOKEN,
28+
message: ERROR_MESSAGE[ERROR_CODE.EXPIRED_ACCESS_TOKEN],
29+
});
30+
return;
31+
}
32+
2333
if (error instanceof TypeError) {
2434
setError(ERROR_CODE.SERVER_ERROR);
2535
onError?.({

frontend/src/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import GlobalStyles from './GlobalStyles';
1111

1212
const queryClient = new QueryClient();
1313

14-
if (process.env.NODE_ENV === 'development') {
15-
const { worker } = require('./mocks/browser');
16-
17-
worker.start();
18-
}
14+
// if (process.env.NODE_ENV === 'development') {
15+
// const { worker } = require('./mocks/browser');
16+
//
17+
// worker.start();
18+
// }
1919

2020
ReactDOM.render(
2121
<React.StrictMode>

0 commit comments

Comments
 (0)