Skip to content

Commit 38e964b

Browse files
authored
Merge pull request #26 from ruiqi7/feature/user-authentication
Attach token in request headers for question create, edit and delete
2 parents 637fb46 + 3f8451a commit 38e964b

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

frontend/src/contexts/AuthContext.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,15 @@ const AuthProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
4242

4343
useEffect(() => {
4444
const accessToken = localStorage.getItem("token");
45-
if (accessToken) {
46-
userClient
47-
.get("/auth/verify-token", {
48-
headers: { Authorization: `Bearer ${accessToken}` },
49-
})
50-
.then((res) => setUser(res.data.data))
51-
.catch(() => setUser(null))
52-
.finally(() => {
53-
setTimeout(() => setLoading(false), 1500)
54-
});
55-
} else {
56-
setTimeout(() => setLoading(false), 1500);
57-
}
45+
userClient
46+
.get("/auth/verify-token", {
47+
headers: { Authorization: `Bearer ${accessToken}` },
48+
})
49+
.then((res) => setUser(res.data.data))
50+
.catch(() => setUser(null))
51+
.finally(() => {
52+
setTimeout(() => setLoading(false), 1000)
53+
});
5854
}, []);
5955

6056
const signup = (

frontend/src/reducers/questionReducer.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,22 @@ export const createQuestion = async (
8686
question: Omit<QuestionDetail, "id">,
8787
dispatch: Dispatch<QuestionActions>
8888
): Promise<boolean> => {
89+
const accessToken = localStorage.getItem("token");
8990
return questionClient
90-
.post("/", {
91-
title: question.title,
92-
description: question.description,
93-
complexity: question.complexity,
94-
category: question.categories,
95-
})
91+
.post(
92+
"/",
93+
{
94+
title: question.title,
95+
description: question.description,
96+
complexity: question.complexity,
97+
category: question.categories,
98+
},
99+
{
100+
headers: {
101+
Authorization: `Bearer ${accessToken}`,
102+
},
103+
}
104+
)
96105
.then((res) => {
97106
dispatch({
98107
type: QuestionActionTypes.CREATE_QUESTION,
@@ -183,13 +192,22 @@ export const updateQuestionById = async (
183192
question: Omit<QuestionDetail, "id">,
184193
dispatch: Dispatch<QuestionActions>
185194
): Promise<boolean> => {
195+
const accessToken = localStorage.getItem("token");
186196
return questionClient
187-
.put(`/${questionId}`, {
188-
title: question.title,
189-
description: question.description,
190-
complexity: question.complexity,
191-
category: question.categories,
192-
})
197+
.put(
198+
`/${questionId}`,
199+
{
200+
title: question.title,
201+
description: question.description,
202+
complexity: question.complexity,
203+
category: question.categories,
204+
},
205+
{
206+
headers: {
207+
Authorization: `Bearer ${accessToken}`,
208+
},
209+
}
210+
)
193211
.then((res) => {
194212
dispatch({
195213
type: QuestionActionTypes.UPDATE_QUESTION,
@@ -208,7 +226,12 @@ export const updateQuestionById = async (
208226

209227
export const deleteQuestionById = async (questionId: string) => {
210228
try {
211-
await questionClient.delete(`/${questionId}`);
229+
const accessToken = localStorage.getItem("token");
230+
await questionClient.delete(`/${questionId}`, {
231+
headers: {
232+
Authorization: `Bearer ${accessToken}`,
233+
},
234+
});
212235
return true;
213236
} catch {
214237
return false;
@@ -229,9 +252,11 @@ export const createImageUrls = async (
229252
formData: FormData
230253
): Promise<{ imageUrls: string[]; message: string } | null> => {
231254
try {
255+
const accessToken = localStorage.getItem("token");
232256
const response = await questionClient.post("/images", formData, {
233257
headers: {
234258
"Content-Type": "multipart/form-data",
259+
Authorization: `Bearer ${accessToken}`,
235260
},
236261
});
237262
return response.data;

0 commit comments

Comments
 (0)