From 3ae3d2053981253439ad7b5ecd86b3f8edd88bed Mon Sep 17 00:00:00 2001 From: Rick Date: Tue, 12 Nov 2024 21:05:52 +0800 Subject: [PATCH] Handle errors for token parsing in auth.ts and services --- frontend/src/lib/auth.ts | 24 +++++++++----- frontend/src/services/authService.ts | 33 ++++++++++--------- frontend/src/services/collaborationService.ts | 9 ++--- frontend/src/services/questionService.ts | 21 ++++-------- frontend/src/services/userService.ts | 3 +- 5 files changed, 44 insertions(+), 46 deletions(-) diff --git a/frontend/src/lib/auth.ts b/frontend/src/lib/auth.ts index ed4c077d0a..3cc998dffe 100644 --- a/frontend/src/lib/auth.ts +++ b/frontend/src/lib/auth.ts @@ -16,11 +16,15 @@ import { export async function getAccessToken(): Promise { "use server"; const cookieStore = await cookies(); - const access_token = AccessTokenSchema.parse( - cookieStore.get("access_token")?.value - ); + try { + const access_token = AccessTokenSchema.parse( + cookieStore.get("access_token")?.value + ); - return access_token; + return access_token; + } catch (error) { + return ""; + } } /** @@ -31,11 +35,15 @@ export async function getAccessToken(): Promise { export async function getRefreshToken(): Promise { "use server"; const cookieStore = await cookies(); - const access_token = AccessTokenSchema.parse( - cookieStore.get("refresh_token")?.value - ); + try { + const access_token = AccessTokenSchema.parse( + cookieStore.get("refresh_token")?.value + ); - return access_token; + return access_token; + } catch (error) { + return ""; + } } /** diff --git a/frontend/src/services/authService.ts b/frontend/src/services/authService.ts index 44bb2618ce..338f8669ae 100644 --- a/frontend/src/services/authService.ts +++ b/frontend/src/services/authService.ts @@ -126,9 +126,8 @@ export async function signup( } export async function logout(): Promise { + const access_token = await getAccessToken(); try { - const access_token = await getAccessToken(); - const res: Response = await fetch( process.env.PUBLIC_API_URL + `/api/auth/logout`, { @@ -155,9 +154,8 @@ export async function logout(): Promise { } export async function refreshAccessToken(): Promise { + const refresh_token = await getRefreshToken(); try { - const refresh_token = await getRefreshToken(); - const res: Response = await fetch( process.env.PUBLIC_API_URL + `/api/auth/refresh`, { @@ -183,13 +181,14 @@ export async function refreshAccessToken(): Promise { } } -export async function resetPassword(email: string): -Promise<{statusCode: number; message: string}> { +export async function resetPassword( + email: string +): Promise<{ statusCode: number; message: string }> { try { const validatedData = ForgotPasswordSchema.parse({ email }); - + const response = await fetch( - process.env.PUBLIC_API_URL + '/api/auth/reset-password', + process.env.PUBLIC_API_URL + "/api/auth/reset-password", { cache: "no-cache", method: "POST", @@ -213,11 +212,12 @@ Promise<{statusCode: number; message: string}> { } } -export async function verifyCode(token: string): -Promise<{statusCode: number; message: string}> { +export async function verifyCode( + token: string +): Promise<{ statusCode: number; message: string }> { try { const response = await fetch( - process.env.PUBLIC_API_URL + '/api/auth/reset-password/verify', + process.env.PUBLIC_API_URL + "/api/auth/reset-password/verify", { cache: "no-cache", method: "POST", @@ -241,18 +241,20 @@ Promise<{statusCode: number; message: string}> { } } -export async function confirmResetPassword(token: string, password: string): -Promise<{statusCode: number; message: string}> { +export async function confirmResetPassword( + token: string, + password: string +): Promise<{ statusCode: number; message: string }> { try { const response = await fetch( - process.env.PUBLIC_API_URL + '/api/auth/reset-password/confirm', + process.env.PUBLIC_API_URL + "/api/auth/reset-password/confirm", { cache: "no-cache", method: "POST", headers: { "Content-Type": "application/json", }, - body: JSON.stringify({token, password}), + body: JSON.stringify({ token, password }), } ); @@ -268,4 +270,3 @@ Promise<{statusCode: number; message: string}> { }; } } - diff --git a/frontend/src/services/collaborationService.ts b/frontend/src/services/collaborationService.ts index 9a06ccd3ca..6a47df2f0f 100644 --- a/frontend/src/services/collaborationService.ts +++ b/frontend/src/services/collaborationService.ts @@ -16,9 +16,8 @@ import { cache } from "react"; export const getSessionInfo = cache(async function ( sessionId: string ): Promise { + const access_token = await getAccessToken(); try { - const access_token = await getAccessToken(); - const res = await fetch( process.env.PUBLIC_API_URL + `/api/collaboration/${sessionId}`, { @@ -44,9 +43,8 @@ export const getSessionInfo = cache(async function ( }); export async function getUserSessionHistory(): Promise { + const access_token = await getAccessToken(); try { - const access_token = await getAccessToken(); - const res = await fetch( process.env.PUBLIC_API_URL + `/api/collaboration/history`, { @@ -73,9 +71,8 @@ export async function createCodeReview( sessionId: string, code: string ): Promise { + const access_token = await getAccessToken(); try { - const access_token = await getAccessToken(); - const res = await fetch( process.env.PUBLIC_API_URL + "/api/collaboration/review", { diff --git a/frontend/src/services/questionService.ts b/frontend/src/services/questionService.ts index 68d0f60f68..67e0d4f920 100644 --- a/frontend/src/services/questionService.ts +++ b/frontend/src/services/questionService.ts @@ -20,9 +20,8 @@ import { cache } from "react"; export const getQuestion = cache(async function ( slug: string ): Promise { + const access_token = await getAccessToken(); try { - const access_token = await getAccessToken(); - const res = await fetch( process.env.PUBLIC_API_URL + `/api/questions/${slug}`, { @@ -51,9 +50,8 @@ export const getQuestions = cache( limit: 99, }); + const access_token = await getAccessToken(); try { - const access_token = await getAccessToken(); - const res: Response = await fetch( process.env.PUBLIC_API_URL + `/api/questions?${query}`, { @@ -80,9 +78,8 @@ export const getQuestions = cache( export const getQuestionCategories = cache( async function (): Promise { + const access_token = await getAccessToken(); try { - const access_token = await getAccessToken(); - const res: Response = await fetch( process.env.PUBLIC_API_URL + `/api/questions/categories`, { @@ -110,9 +107,8 @@ export const getQuestionCategories = cache( export async function createQuestion( question: NewQuestion ): Promise { + const access_token = await getAccessToken(); try { - const access_token = await getAccessToken(); - const res = await fetch( process.env.PUBLIC_API_URL + "/api/questions/create", { @@ -139,9 +135,8 @@ export async function createQuestion( } export async function deleteQuestion(questionId: string): Promise { + const access_token = await getAccessToken(); try { - const access_token = await getAccessToken(); - await fetch(process.env.PUBLIC_API_URL + `/api/questions/${questionId}`, { method: "DELETE", headers: { @@ -157,9 +152,8 @@ export async function deleteQuestion(questionId: string): Promise { export async function editQuestion( question: EditQuestion ): Promise { + const access_token = await getAccessToken(); try { - const access_token = await getAccessToken(); - const updatedQuestion = NewQuestionSchema.parse(question); const res = await fetch( process.env.PUBLIC_API_URL + `/api/questions/${question._id}`, @@ -190,9 +184,8 @@ export async function updateQuestionTestCases( questionId: string, testCases: Array ): Promise { + const access_token = await getAccessToken(); try { - const access_token = await getAccessToken(); - const res = await fetch( process.env.PUBLIC_API_URL + `/api/questions/${questionId}/testcases`, { diff --git a/frontend/src/services/userService.ts b/frontend/src/services/userService.ts index 7ba79a0d59..c63f652f9c 100644 --- a/frontend/src/services/userService.ts +++ b/frontend/src/services/userService.ts @@ -43,9 +43,8 @@ export const getCurrentUser = cache( export async function editUserProfile( userProfile: UserProfile ): Promise { + const access_token = await getAccessToken(); try { - const access_token = await getAccessToken(); - const updatedUserProfile = UpdateUserProfileSchema.parse(userProfile); const res = await fetch(process.env.PUBLIC_API_URL + `/api/users/profile`, {