Skip to content

Commit 2046276

Browse files
committed
Combined auth error message generator, removed redundant type
1 parent ae7c384 commit 2046276

File tree

6 files changed

+14
-34
lines changed

6 files changed

+14
-34
lines changed

frontend/src/APIClients/AuthAPIClient.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {
55
} from "@apollo/client";
66
import { AxiosError } from "axios";
77
import {
8-
getLoginErrMessage,
9-
getRegisterErrMessage
8+
getAuthErrMessage
109
}
1110
from "../helper/authError";
1211
import AUTHENTICATED_USER_KEY from "../constants/AuthConstants";
@@ -37,7 +36,7 @@ const login = async (
3736
if (axiosErr.response && axiosErr.response.status === 401) {
3837
return {
3938
errCode: axiosErr.response.status,
40-
errMessage: getLoginErrMessage(axiosErr.response),
39+
errMessage: getAuthErrMessage(axiosErr.response, 'LOGIN'),
4140
};
4241
}
4342
return {
@@ -116,7 +115,7 @@ const register = async (
116115
if (axiosErr.response && axiosErr.response.status === 409) {
117116
return {
118117
errCode: axiosErr.response.status,
119-
errMessage: getRegisterErrMessage(axiosErr.response),
118+
errMessage: getAuthErrMessage(axiosErr.response, 'SIGNUP'),
120119
};
121120
}
122121
return null;

frontend/src/APIClients/CommonAPIClient.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AxiosError } from "axios";
22
import AUTHENTICATED_USER_KEY from "../constants/AuthConstants";
33
import { getLocalStorageObjProperty } from "../utils/LocalStorageUtils";
44
import baseAPIClient from "./BaseAPIClient";
5-
import { UserStatusErrorResponse } from '../types/UserTypes'
5+
import { ErrorResponse } from "../types/AuthTypes";
66

77
const inviteUser = async (
88
email: string,
@@ -26,7 +26,7 @@ const inviteUser = async (
2626
}
2727
};
2828

29-
const getUserStatus = async (email: string): Promise<string | UserStatusErrorResponse> => {
29+
const getUserStatus = async (email: string): Promise<string | ErrorResponse> => {
3030
try {
3131
if (email === "") {
3232
return "";

frontend/src/components/forms/Signup.tsx

+1-9
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,8 @@ import { HOME_PAGE, LOGIN_PAGE } from "../../constants/Routes";
1414
import AuthContext from "../../contexts/AuthContext";
1515
import commonApiClient from "../../APIClients/CommonAPIClient";
1616
import AUTHENTICATED_USER_KEY from "../../constants/AuthConstants";
17-
import { UserStatusErrorResponse } from "../../types/UserTypes";
18-
import { ErrorResponse } from "../../types/AuthTypes";
1917
import { isAuthErrorResponse } from "../../helper/authError";
2018

21-
const isUserStatusErrorResponse = (
22-
res: string | UserStatusErrorResponse,
23-
): res is UserStatusErrorResponse => {
24-
return res !== null && typeof res !== 'string' && "errCode" in res;
25-
};
26-
2719
type SignupProps = {
2820
email: string;
2921
setEmail: (email: string) => void;
@@ -88,7 +80,7 @@ const Signup = ({
8880

8981
const isInvited = await commonApiClient.isUserInvited(email);
9082
if (isInvited !== "Not Invited") {
91-
if (isUserStatusErrorResponse(isInvited)) {
83+
if (isAuthErrorResponse(isInvited)) {
9284
setEmailErrorStr(isInvited.errMessage)
9385
setEmailError(true)
9486
}

frontend/src/helper/authError.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
import { AxiosError } from "axios";
2-
import { AuthTokenResponse, ErrorResponse } from "../types/AuthTypes";
2+
import { AuthTokenResponse, ErrorResponse, AuthFlow } from "../types/AuthTypes";
33

4-
// Helper to get login error message
5-
export const getLoginErrMessage = (axiosErrRes: AxiosError["response"]): string => {
4+
export const getAuthErrMessage = (axiosErrRes: AxiosError["response"], flow: AuthFlow): string => {
65
if (axiosErrRes && axiosErrRes.data && axiosErrRes.data.error) {
76
return axiosErrRes.data.error;
87
}
9-
return "Error logging in. Please try again later.";
10-
};
11-
12-
export const getRegisterErrMessage = (axiosErrRes: AxiosError["response"]): string => {
13-
if (axiosErrRes && axiosErrRes.data && axiosErrRes.data.error) {
14-
return axiosErrRes.data.error;
15-
}
16-
return "Error signing up. Please try again later.";
17-
};
8+
return `Error ${flow === 'LOGIN' ? "logging in" : "signing up"}. Please try again later.`;
9+
}
1810

1911
export const isAuthErrorResponse = (
20-
res: AuthTokenResponse | ErrorResponse,
12+
res: string | AuthTokenResponse | ErrorResponse,
2113
): res is ErrorResponse => {
22-
return res !== null && "errCode" in res;
14+
return res !== null && typeof res !== 'string' && "errCode" in res;
2315
};

frontend/src/types/AuthTypes.ts

+2
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ export type ErrorResponse = {
2727
errCode: number;
2828
errMessage: string;
2929
};
30+
31+
export type AuthFlow = 'LOGIN' | 'SIGNUP';

frontend/src/types/UserTypes.ts

-5
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,3 @@ export enum UserStatus {
3434
ACTIVE = "Active",
3535
DEACTIVATED = "Deactivated",
3636
}
37-
38-
export type UserStatusErrorResponse = {
39-
errCode: number;
40-
errMessage: string;
41-
}

0 commit comments

Comments
 (0)