2024-05-08 17:31:20 +00:00
|
|
|
import { ReactNode } from "react";
|
2024-05-14 08:16:05 +00:00
|
|
|
import Link from "next/link";
|
2024-05-23 08:34:53 +00:00
|
|
|
// helpers
|
|
|
|
import { SUPPORT_EMAIL } from "./common.helper";
|
2024-05-08 17:31:20 +00:00
|
|
|
|
|
|
|
export enum EPageTypes {
|
2024-05-14 08:16:05 +00:00
|
|
|
PUBLIC = "PUBLIC",
|
|
|
|
NON_AUTHENTICATED = "NON_AUTHENTICATED",
|
|
|
|
SET_PASSWORD = "SET_PASSWORD",
|
|
|
|
ONBOARDING = "ONBOARDING",
|
|
|
|
AUTHENTICATED = "AUTHENTICATED",
|
2024-05-08 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export enum EAuthModes {
|
|
|
|
SIGN_IN = "SIGN_IN",
|
|
|
|
SIGN_UP = "SIGN_UP",
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum EAuthSteps {
|
|
|
|
EMAIL = "EMAIL",
|
|
|
|
PASSWORD = "PASSWORD",
|
|
|
|
UNIQUE_CODE = "UNIQUE_CODE",
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum EErrorAlertType {
|
|
|
|
BANNER_ALERT = "BANNER_ALERT",
|
|
|
|
INLINE_FIRST_NAME = "INLINE_FIRST_NAME",
|
|
|
|
INLINE_EMAIL = "INLINE_EMAIL",
|
|
|
|
INLINE_PASSWORD = "INLINE_PASSWORD",
|
|
|
|
INLINE_EMAIL_CODE = "INLINE_EMAIL_CODE",
|
|
|
|
}
|
|
|
|
|
2024-05-14 08:16:05 +00:00
|
|
|
export enum EAuthenticationErrorCodes {
|
|
|
|
// Admin
|
|
|
|
ADMIN_ALREADY_EXIST = "5150",
|
|
|
|
REQUIRED_ADMIN_EMAIL_PASSWORD_FIRST_NAME = "5155",
|
|
|
|
INVALID_ADMIN_EMAIL = "5160",
|
|
|
|
INVALID_ADMIN_PASSWORD = "5165",
|
|
|
|
REQUIRED_ADMIN_EMAIL_PASSWORD = "5170",
|
|
|
|
ADMIN_AUTHENTICATION_FAILED = "5175",
|
|
|
|
ADMIN_USER_ALREADY_EXIST = "5180",
|
|
|
|
ADMIN_USER_DOES_NOT_EXIST = "5185",
|
2024-05-23 08:34:53 +00:00
|
|
|
ADMIN_USER_DEACTIVATED = "5190",
|
2024-05-14 08:16:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
export type TAuthErrorInfo = {
|
|
|
|
type: EErrorAlertType;
|
|
|
|
code: EAuthenticationErrorCodes;
|
|
|
|
title: string;
|
|
|
|
message: ReactNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
const errorCodeMessages: {
|
|
|
|
[key in EAuthenticationErrorCodes]: { title: string; message: (email?: string | undefined) => ReactNode };
|
|
|
|
} = {
|
2024-05-14 08:16:05 +00:00
|
|
|
// admin
|
2024-05-08 17:31:20 +00:00
|
|
|
[EAuthenticationErrorCodes.ADMIN_ALREADY_EXIST]: {
|
2024-05-14 08:16:05 +00:00
|
|
|
title: `Admin already exists`,
|
|
|
|
message: () => `Admin already exists. Please try again.`,
|
2024-05-08 17:31:20 +00:00
|
|
|
},
|
|
|
|
[EAuthenticationErrorCodes.REQUIRED_ADMIN_EMAIL_PASSWORD_FIRST_NAME]: {
|
2024-05-14 08:16:05 +00:00
|
|
|
title: `Email, password and first name required`,
|
|
|
|
message: () => `Email, password and first name required. Please try again.`,
|
2024-05-08 17:31:20 +00:00
|
|
|
},
|
|
|
|
[EAuthenticationErrorCodes.INVALID_ADMIN_EMAIL]: {
|
2024-05-14 08:16:05 +00:00
|
|
|
title: `Invalid admin email`,
|
|
|
|
message: () => `Invalid admin email. Please try again.`,
|
2024-05-08 17:31:20 +00:00
|
|
|
},
|
|
|
|
[EAuthenticationErrorCodes.INVALID_ADMIN_PASSWORD]: {
|
2024-05-14 08:16:05 +00:00
|
|
|
title: `Invalid admin password`,
|
|
|
|
message: () => `Invalid admin password. Please try again.`,
|
2024-05-08 17:31:20 +00:00
|
|
|
},
|
|
|
|
[EAuthenticationErrorCodes.REQUIRED_ADMIN_EMAIL_PASSWORD]: {
|
2024-05-14 08:16:05 +00:00
|
|
|
title: `Email and password required`,
|
|
|
|
message: () => `Email and password required. Please try again.`,
|
2024-05-08 17:31:20 +00:00
|
|
|
},
|
|
|
|
[EAuthenticationErrorCodes.ADMIN_AUTHENTICATION_FAILED]: {
|
2024-05-14 08:16:05 +00:00
|
|
|
title: `Authentication failed`,
|
|
|
|
message: () => `Authentication failed. Please try again.`,
|
2024-05-08 17:31:20 +00:00
|
|
|
},
|
|
|
|
[EAuthenticationErrorCodes.ADMIN_USER_ALREADY_EXIST]: {
|
2024-05-14 08:16:05 +00:00
|
|
|
title: `Admin user already exists`,
|
|
|
|
message: () => (
|
|
|
|
<div>
|
|
|
|
Admin user already exists.
|
|
|
|
<Link className="underline underline-offset-4 font-medium hover:font-bold transition-all" href={`/admin`}>
|
|
|
|
Sign In
|
|
|
|
</Link>
|
|
|
|
now.
|
|
|
|
</div>
|
|
|
|
),
|
2024-05-08 17:31:20 +00:00
|
|
|
},
|
|
|
|
[EAuthenticationErrorCodes.ADMIN_USER_DOES_NOT_EXIST]: {
|
2024-05-14 08:16:05 +00:00
|
|
|
title: `Admin user does not exist`,
|
|
|
|
message: () => (
|
|
|
|
<div>
|
|
|
|
Admin user does not exist.
|
|
|
|
<Link className="underline underline-offset-4 font-medium hover:font-bold transition-all" href={`/admin`}>
|
|
|
|
Sign In
|
|
|
|
</Link>
|
|
|
|
now.
|
|
|
|
</div>
|
|
|
|
),
|
2024-05-08 17:31:20 +00:00
|
|
|
},
|
2024-05-23 08:34:53 +00:00
|
|
|
[EAuthenticationErrorCodes.ADMIN_USER_DEACTIVATED]: {
|
|
|
|
title: `User account deactivated`,
|
|
|
|
message: () => `User account deactivated. Please contact ${!!SUPPORT_EMAIL ? SUPPORT_EMAIL : "administrator"}.`,
|
|
|
|
},
|
2024-05-08 17:31:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const authErrorHandler = (
|
|
|
|
errorCode: EAuthenticationErrorCodes,
|
|
|
|
email?: string | undefined
|
|
|
|
): TAuthErrorInfo | undefined => {
|
|
|
|
const bannerAlertErrorCodes = [
|
2024-05-23 08:34:53 +00:00
|
|
|
EAuthenticationErrorCodes.ADMIN_ALREADY_EXIST,
|
2024-05-08 17:31:20 +00:00
|
|
|
EAuthenticationErrorCodes.REQUIRED_ADMIN_EMAIL_PASSWORD_FIRST_NAME,
|
2024-05-14 08:16:05 +00:00
|
|
|
EAuthenticationErrorCodes.INVALID_ADMIN_EMAIL,
|
|
|
|
EAuthenticationErrorCodes.INVALID_ADMIN_PASSWORD,
|
2024-05-08 17:31:20 +00:00
|
|
|
EAuthenticationErrorCodes.REQUIRED_ADMIN_EMAIL_PASSWORD,
|
2024-05-14 08:16:05 +00:00
|
|
|
EAuthenticationErrorCodes.ADMIN_AUTHENTICATION_FAILED,
|
2024-05-08 17:31:20 +00:00
|
|
|
EAuthenticationErrorCodes.ADMIN_USER_ALREADY_EXIST,
|
|
|
|
EAuthenticationErrorCodes.ADMIN_USER_DOES_NOT_EXIST,
|
2024-05-23 08:34:53 +00:00
|
|
|
EAuthenticationErrorCodes.ADMIN_USER_DEACTIVATED,
|
2024-05-08 17:31:20 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if (bannerAlertErrorCodes.includes(errorCode))
|
|
|
|
return {
|
|
|
|
type: EErrorAlertType.BANNER_ALERT,
|
|
|
|
code: errorCode,
|
|
|
|
title: errorCodeMessages[errorCode]?.title || "Error",
|
|
|
|
message: errorCodeMessages[errorCode]?.message(email) || "Something went wrong. Please try again.",
|
|
|
|
};
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
};
|