Skip to content

Commit

Permalink
Merge pull request #130 from CS3219-AY2425S1/main-rick/patches
Browse files Browse the repository at this point in the history
Handle errors for token parsing in auth.ts and services
  • Loading branch information
rickkoh authored Nov 12, 2024
2 parents 98cdf08 + 3ae3d20 commit 518c351
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 46 deletions.
24 changes: 16 additions & 8 deletions frontend/src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ import {
export async function getAccessToken(): Promise<AccessToken> {
"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 "";
}
}

/**
Expand All @@ -31,11 +35,15 @@ export async function getAccessToken(): Promise<AccessToken> {
export async function getRefreshToken(): Promise<RefreshToken> {
"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 "";
}
}

/**
Expand Down
33 changes: 17 additions & 16 deletions frontend/src/services/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,8 @@ export async function signup(
}

export async function logout(): Promise<LogoutResponse> {
const access_token = await getAccessToken();
try {
const access_token = await getAccessToken();

const res: Response = await fetch(
process.env.PUBLIC_API_URL + `/api/auth/logout`,
{
Expand All @@ -155,9 +154,8 @@ export async function logout(): Promise<LogoutResponse> {
}

export async function refreshAccessToken(): Promise<TokenPairResponse> {
const refresh_token = await getRefreshToken();
try {
const refresh_token = await getRefreshToken();

const res: Response = await fetch(
process.env.PUBLIC_API_URL + `/api/auth/refresh`,
{
Expand All @@ -183,13 +181,14 @@ export async function refreshAccessToken(): Promise<TokenPairResponse> {
}
}

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",
Expand All @@ -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",
Expand All @@ -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 }),
}
);

Expand All @@ -268,4 +270,3 @@ Promise<{statusCode: number; message: string}> {
};
}
}

9 changes: 3 additions & 6 deletions frontend/src/services/collaborationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import { cache } from "react";
export const getSessionInfo = cache(async function (
sessionId: string
): Promise<SessionInfoResponse> {
const access_token = await getAccessToken();
try {
const access_token = await getAccessToken();

const res = await fetch(
process.env.PUBLIC_API_URL + `/api/collaboration/${sessionId}`,
{
Expand All @@ -44,9 +43,8 @@ export const getSessionInfo = cache(async function (
});

export async function getUserSessionHistory(): Promise<HistorySessionInfoResponse> {
const access_token = await getAccessToken();
try {
const access_token = await getAccessToken();

const res = await fetch(
process.env.PUBLIC_API_URL + `/api/collaboration/history`,
{
Expand All @@ -73,9 +71,8 @@ export async function createCodeReview(
sessionId: string,
code: string
): Promise<CodeReviewResponse> {
const access_token = await getAccessToken();
try {
const access_token = await getAccessToken();

const res = await fetch(
process.env.PUBLIC_API_URL + "/api/collaboration/review",
{
Expand Down
21 changes: 7 additions & 14 deletions frontend/src/services/questionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import { cache } from "react";
export const getQuestion = cache(async function (
slug: string
): Promise<QuestionResponse> {
const access_token = await getAccessToken();
try {
const access_token = await getAccessToken();

const res = await fetch(
process.env.PUBLIC_API_URL + `/api/questions/${slug}`,
{
Expand Down Expand Up @@ -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}`,
{
Expand All @@ -80,9 +78,8 @@ export const getQuestions = cache(

export const getQuestionCategories = cache(
async function (): Promise<CategoriesResponse> {
const access_token = await getAccessToken();
try {
const access_token = await getAccessToken();

const res: Response = await fetch(
process.env.PUBLIC_API_URL + `/api/questions/categories`,
{
Expand Down Expand Up @@ -110,9 +107,8 @@ export const getQuestionCategories = cache(
export async function createQuestion(
question: NewQuestion
): Promise<QuestionResponse> {
const access_token = await getAccessToken();
try {
const access_token = await getAccessToken();

const res = await fetch(
process.env.PUBLIC_API_URL + "/api/questions/create",
{
Expand All @@ -139,9 +135,8 @@ export async function createQuestion(
}

export async function deleteQuestion(questionId: string): Promise<void> {
const access_token = await getAccessToken();
try {
const access_token = await getAccessToken();

await fetch(process.env.PUBLIC_API_URL + `/api/questions/${questionId}`, {
method: "DELETE",
headers: {
Expand All @@ -157,9 +152,8 @@ export async function deleteQuestion(questionId: string): Promise<void> {
export async function editQuestion(
question: EditQuestion
): Promise<QuestionResponse> {
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}`,
Expand Down Expand Up @@ -190,9 +184,8 @@ export async function updateQuestionTestCases(
questionId: string,
testCases: Array<TestCase>
): Promise<QuestionResponse> {
const access_token = await getAccessToken();
try {
const access_token = await getAccessToken();

const res = await fetch(
process.env.PUBLIC_API_URL + `/api/questions/${questionId}/testcases`,
{
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ export const getCurrentUser = cache(
export async function editUserProfile(
userProfile: UserProfile
): Promise<UserProfileResponse> {
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`, {
Expand Down

0 comments on commit 518c351

Please sign in to comment.