plane/web/helpers/authentication.helper.ts

117 lines
3.8 KiB
TypeScript
Raw Normal View History

export enum EPageTypes {
"PUBLIC" = "PUBLIC",
"NON_AUTHENTICATED" = "NON_AUTHENTICATED",
"ONBOARDING" = "ONBOARDING",
"AUTHENTICATED" = "AUTHENTICATED",
}
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 };
2024-04-30 14:00:42 +00:00
export const authErrorHandler = (
errorCode: EAuthenticationErrorCodes,
errorMessage: string | undefined
): TAuthErrorInfo | undefined => {
2024-04-30 11:34:49 +00:00
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];
2024-04-30 14:00:42 +00:00
if (toastAlertErrorCodes.includes(errorCode))
return {
2024-04-30 11:34:49 +00:00
type: EErrorAlertType.TOAST_ALERT,
2024-04-30 14:00:42 +00:00
message: errorMessage || "Something went wrong. Please try again.",
2024-04-30 11:34:49 +00:00
};
2024-04-30 14:00:42 +00:00
if (bannerAlertErrorCodes.includes(errorCode))
return {
2024-04-30 11:34:49 +00:00
type: EErrorAlertType.BANNER_ALERT,
2024-04-30 14:00:42 +00:00
message: errorMessage || "Something went wrong. Please try again.",
2024-04-30 11:34:49 +00:00
};
2024-04-30 14:00:42 +00:00
if (inlineFirstNameErrorCodes.includes(errorCode))
return {
2024-04-30 11:34:49 +00:00
type: EErrorAlertType.INLINE_FIRST_NAME,
2024-04-30 14:00:42 +00:00
message: errorMessage || "Something went wrong. Please try again.",
2024-04-30 11:34:49 +00:00
};
2024-04-30 14:00:42 +00:00
if (inlineEmailErrorCodes.includes(errorCode))
return {
2024-04-30 11:34:49 +00:00
type: EErrorAlertType.INLINE_EMAIL,
2024-04-30 14:00:42 +00:00
message: errorMessage || "Something went wrong. Please try again.",
2024-04-30 11:34:49 +00:00
};
2024-04-30 14:00:42 +00:00
if (inlinePasswordErrorCodes.includes(errorCode))
return {
2024-04-30 11:34:49 +00:00
type: EErrorAlertType.INLINE_PASSWORD,
2024-04-30 14:00:42 +00:00
message: errorMessage || "Something went wrong. Please try again.",
2024-04-30 11:34:49 +00:00
};
2024-04-30 14:00:42 +00:00
if (inlineEmailCodeErrorCodes.includes(errorCode))
return {
2024-04-30 11:34:49 +00:00
type: EErrorAlertType.INLINE_EMAIL_CODE,
2024-04-30 14:00:42 +00:00
message: errorMessage || "Something went wrong. Please try again.",
2024-04-30 11:34:49 +00:00
};
2024-04-29 17:42:43 +00:00
2024-04-30 14:00:42 +00:00
return undefined;
2024-04-29 17:42:43 +00:00
};