Skip to content

Commit f490f9a

Browse files
committed
Refactor code
1 parent 4f18fb7 commit f490f9a

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

frontend/src/contexts/AuthContext.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { toast } from "react-toastify";
77
import Loader from "../components/Loader";
88
import { SUCCESS_LOG_OUT } from "../utils/constants";
99
import { User } from "../types/types";
10+
import { getToken, removeToken, setToken } from "../utils/token";
1011

1112
type AuthContextType = {
1213
signup: (
@@ -32,11 +33,11 @@ const AuthProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
3233
const navigate = useNavigate();
3334

3435
useEffect(() => {
35-
const accessToken = localStorage.getItem("token");
36+
const accessToken = getToken();
3637
if (accessToken) {
3738
userClient
3839
.get("/auth/verify-token", {
39-
headers: { Authorization: `Bearer ${accessToken}` },
40+
headers: { Authorization: accessToken },
4041
})
4142
.then((res) => setUser(res.data.data))
4243
.catch(() => setUser(null))
@@ -82,7 +83,7 @@ const AuthProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
8283
})
8384
.then((res) => {
8485
const { accessToken, user } = res.data.data;
85-
localStorage.setItem("token", accessToken);
86+
setToken(accessToken);
8687
setUser(user);
8788
navigate("/home");
8889
})
@@ -96,7 +97,7 @@ const AuthProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
9697
};
9798

9899
const logout = () => {
99-
localStorage.removeItem("token");
100+
removeToken();
100101
setUser(null);
101102
navigate("/");
102103
toast.success(SUCCESS_LOG_OUT);

frontend/src/contexts/ProfileContext.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "../utils/constants";
1111
import { toast } from "react-toastify";
1212
import axios from "axios";
13+
import { getToken } from "../utils/token";
1314

1415
interface UserProfileBase {
1516
firstName: string;
@@ -81,10 +82,10 @@ const ProfileContextProvider: React.FC<{ children: React.ReactNode }> = ({
8182
};
8283

8384
const updateProfile = async (data: UserProfileBase): Promise<boolean> => {
84-
const token = localStorage.getItem("token");
85+
const token = getToken();
8586
try {
8687
const res = await userClient.patch(`/users/${user?.id}`, data, {
87-
headers: { Authorization: `Bearer ${token}` },
88+
headers: { Authorization: token },
8889
});
8990
setUser(res.data.data);
9091
toast.success(SUCCESS_PROFILE_UPDATE_MESSAGE);
@@ -110,12 +111,12 @@ const ProfileContextProvider: React.FC<{ children: React.ReactNode }> = ({
110111
oldPassword: string;
111112
newPassword: string;
112113
}) => {
113-
const token = localStorage.getItem("token");
114+
const token = getToken();
114115
await userClient
115116
.patch(
116117
`/users/${user?.id}`,
117118
{ oldPassword, newPassword },
118-
{ headers: { Authorization: `Bearer ${token}` } }
119+
{ headers: { Authorization: token } }
119120
)
120121
.then(() => toast.success(SUCCESS_PW_UPDATE_MESSAGE))
121122
.catch((err) => {

frontend/src/reducers/questionReducer.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Dispatch } from "react";
22
import { questionClient } from "../utils/api";
33
import { isString, isStringArray } from "../utils/typeChecker";
4+
import { getToken } from "../utils/token";
45

56
type TestcaseFiles = {
67
testcaseInputFile: File | null;
@@ -126,11 +127,11 @@ export const uploadTestcaseFiles = async (
126127
formData.append("requestType", requestType);
127128

128129
try {
129-
const accessToken = localStorage.getItem("token");
130+
const accessToken = getToken();
130131
const res = await questionClient.post("/tcfiles", formData, {
131132
headers: {
132133
"Content-Type": "multipart/form-data",
133-
Authorization: `Bearer ${accessToken}`,
134+
Authorization: accessToken,
134135
},
135136
});
136137
return res.data;
@@ -159,7 +160,7 @@ export const createQuestion = async (
159160

160161
const { testcaseInputFileUrl, testcaseOutputFileUrl } = uploadResult.urls;
161162

162-
const accessToken = localStorage.getItem("token");
163+
const accessToken = getToken();
163164
return questionClient
164165
.post(
165166
"/",
@@ -176,7 +177,7 @@ export const createQuestion = async (
176177
},
177178
{
178179
headers: {
179-
Authorization: `Bearer ${accessToken}`,
180+
Authorization: accessToken,
180181
},
181182
}
182183
)
@@ -297,7 +298,7 @@ export const updateQuestionById = async (
297298
};
298299
}
299300

300-
const accessToken = localStorage.getItem("token");
301+
const accessToken = getToken();
301302
return questionClient
302303
.put(
303304
`/${questionId}`,
@@ -315,7 +316,7 @@ export const updateQuestionById = async (
315316
},
316317
{
317318
headers: {
318-
Authorization: `Bearer ${accessToken}`,
319+
Authorization: accessToken,
319320
},
320321
}
321322
)
@@ -337,10 +338,10 @@ export const updateQuestionById = async (
337338

338339
export const deleteQuestionById = async (questionId: string) => {
339340
try {
340-
const accessToken = localStorage.getItem("token");
341+
const accessToken = getToken();
341342
await questionClient.delete(`/${questionId}`, {
342343
headers: {
343-
Authorization: `Bearer ${accessToken}`,
344+
Authorization: accessToken,
344345
},
345346
});
346347
return true;
@@ -363,11 +364,11 @@ export const createImageUrls = async (
363364
formData: FormData
364365
): Promise<{ imageUrls: string[]; message: string } | null> => {
365366
try {
366-
const accessToken = localStorage.getItem("token");
367+
const accessToken = getToken();
367368
const response = await questionClient.post("/images", formData, {
368369
headers: {
369370
"Content-Type": "multipart/form-data",
370-
Authorization: `Bearer ${accessToken}`,
371+
Authorization: accessToken,
371372
},
372373
});
373374
return response.data;

frontend/src/utils/token.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
export const setToken = (token: string) => {
2-
const bearerToken = `Bearer ${token}`;
3-
localStorage.setItem("accessToken", bearerToken);
2+
localStorage.setItem("accessToken", token);
43
};
54

65
export const getToken = () => {
76
const token = localStorage.getItem("accessToken");
8-
return token;
7+
const bearerToken = `Bearer ${token}`;
8+
return bearerToken;
99
};
1010

1111
export const removeToken = () => {

0 commit comments

Comments
 (0)