plane/web/helpers/authentication.helper.ts

109 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-04-30 11:34:49 +00:00
export enum EAuthModes {
SIGN_IN = "SIGN_IN",
SIGN_UP = "SIGN_UP",
2024-04-29 17:42:43 +00:00
}
2024-04-30 11:34:49 +00:00
export enum EAuthSteps {
EMAIL = "EMAIL",
PASSWORD = "PASSWORD",
UNIQUE_CODE = "UNIQUE_CODE",
2024-04-29 17:42:43 +00:00
}
2024-04-30 11:34:49 +00:00
export enum EAuthenticationErrorCodes {
// alert errors
2024-04-29 17:42:43 +00:00
INSTANCE_NOT_CONFIGURED = "INSTANCE_NOT_CONFIGURED",
2024-04-30 11:34:49 +00:00
SMTP_NOT_CONFIGURED = "SMTP_NOT_CONFIGURED",
AUTHENTICATION_FAILED = "AUTHENTICATION_FAILED",
INVALID_TOKEN = "INVALID_TOKEN",
EXPIRED_TOKEN = "EXPIRED_TOKEN",
IMPROPERLY_CONFIGURED = "IMPROPERLY_CONFIGURED",
OAUTH_PROVIDER_ERROR = "OAUTH_PROVIDER_ERROR",
// banner errors
2024-04-29 17:42:43 +00:00
INVALID_EMAIL = "INVALID_EMAIL",
2024-04-30 11:34:49 +00:00
INVALID_PASSWORD = "INVALID_PASSWORD",
USER_DOES_NOT_EXIST = "USER_DOES_NOT_EXIST",
ADMIN_ALREADY_EXIST = "ADMIN_ALREADY_EXIST",
2024-04-29 17:42:43 +00:00
USER_ALREADY_EXIST = "USER_ALREADY_EXIST",
2024-04-30 11:34:49 +00:00
// inline errors from backend
REQUIRED_EMAIL_PASSWORD_FIRST_NAME = "REQUIRED_EMAIL_PASSWORD_FIRST_NAME",
2024-04-29 17:42:43 +00:00
REQUIRED_EMAIL_PASSWORD = "REQUIRED_EMAIL_PASSWORD",
2024-04-30 11:34:49 +00:00
EMAIL_CODE_REQUIRED = "EMAIL_CODE_REQUIRED",
// inline local errors
INLINE_EMAIL = "INLINE_EMAIL",
INLINE_PASSWORD = "INLINE_PASSWORD",
INLINE_FIRST_NAME = "INLINE_FIRST_NAME",
INLINE_EMAIL_CODE = "INLINE_EMAIL_CODE",
2024-04-29 17:42:43 +00:00
}
export enum EErrorAlertType {
BANNER_ALERT = "BANNER_ALERT",
TOAST_ALERT = "TOAST_ALERT",
INLINE_FIRST_NAME = "INLINE_FIRST_NAME",
INLINE_EMAIL = "INLINE_EMAIL",
INLINE_PASSWORD = "INLINE_PASSWORD",
2024-04-30 11:34:49 +00:00
INLINE_EMAIL_CODE = "INLINE_EMAIL_CODE",
2024-04-29 17:42:43 +00:00
}
2024-04-30 11:34:49 +00:00
export type TAuthErrorInfo = { type: EErrorAlertType; message: string };
export const errorHandler = (errorType: EAuthenticationErrorCodes, errorMessage: string | undefined) => {
const toastAlertErrorCodes = [
EAuthenticationErrorCodes.INSTANCE_NOT_CONFIGURED,
EAuthenticationErrorCodes.SMTP_NOT_CONFIGURED,
EAuthenticationErrorCodes.AUTHENTICATION_FAILED,
EAuthenticationErrorCodes.INVALID_TOKEN,
EAuthenticationErrorCodes.EXPIRED_TOKEN,
EAuthenticationErrorCodes.IMPROPERLY_CONFIGURED,
EAuthenticationErrorCodes.OAUTH_PROVIDER_ERROR,
];
const bannerAlertErrorCodes = [
EAuthenticationErrorCodes.USER_DOES_NOT_EXIST,
EAuthenticationErrorCodes.ADMIN_ALREADY_EXIST,
EAuthenticationErrorCodes.USER_ALREADY_EXIST,
];
const inlineFirstNameErrorCodes = [EAuthenticationErrorCodes.INLINE_FIRST_NAME];
const inlineEmailErrorCodes = [EAuthenticationErrorCodes.INLINE_EMAIL];
const inlineEmailCodeErrorCodes = [EAuthenticationErrorCodes.INLINE_EMAIL_CODE];
const inlinePasswordErrorCodes = [EAuthenticationErrorCodes.INLINE_PASSWORD];
let errorPayload: TAuthErrorInfo | undefined = undefined;
if (toastAlertErrorCodes.includes(errorType))
errorPayload = {
type: EErrorAlertType.TOAST_ALERT,
message: errorMessage || "Something went wrong",
};
if (bannerAlertErrorCodes.includes(errorType))
errorPayload = {
type: EErrorAlertType.BANNER_ALERT,
message: errorMessage || "Something went wrong",
};
if (inlineFirstNameErrorCodes.includes(errorType))
errorPayload = {
type: EErrorAlertType.INLINE_FIRST_NAME,
message: errorMessage || "Something went wrong",
};
if (inlineEmailErrorCodes.includes(errorType))
errorPayload = {
type: EErrorAlertType.INLINE_EMAIL,
message: errorMessage || "Something went wrong",
};
if (inlinePasswordErrorCodes.includes(errorType))
errorPayload = {
type: EErrorAlertType.INLINE_PASSWORD,
message: errorMessage || "Something went wrong",
};
if (inlineEmailCodeErrorCodes.includes(errorType))
errorPayload = {
type: EErrorAlertType.INLINE_EMAIL_CODE,
message: errorMessage || "Something went wrong",
};
2024-04-29 17:42:43 +00:00
return errorPayload;
};