forked from github/plane
296 lines
9.9 KiB
TypeScript
296 lines
9.9 KiB
TypeScript
|
import { ReactNode } from "react";
|
||
|
import Link from "next/link";
|
||
|
|
||
|
export enum EPageTypes {
|
||
|
PUBLIC = "PUBLIC",
|
||
|
NON_AUTHENTICATED = "NON_AUTHENTICATED",
|
||
|
SET_PASSWORD = "SET_PASSWORD",
|
||
|
ONBOARDING = "ONBOARDING",
|
||
|
AUTHENTICATED = "AUTHENTICATED",
|
||
|
}
|
||
|
|
||
|
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",
|
||
|
TOAST_ALERT = "TOAST_ALERT",
|
||
|
INLINE_FIRST_NAME = "INLINE_FIRST_NAME",
|
||
|
INLINE_EMAIL = "INLINE_EMAIL",
|
||
|
INLINE_PASSWORD = "INLINE_PASSWORD",
|
||
|
INLINE_EMAIL_CODE = "INLINE_EMAIL_CODE",
|
||
|
}
|
||
|
|
||
|
export enum EAuthenticationErrorCodes {
|
||
|
// Global
|
||
|
INSTANCE_NOT_CONFIGURED = "5000",
|
||
|
SIGNUP_DISABLED = "5001",
|
||
|
INVALID_PASSWORD = "5002", // Password strength validation
|
||
|
SMTP_NOT_CONFIGURED = "5007",
|
||
|
// email check
|
||
|
INVALID_EMAIL = "5012",
|
||
|
EMAIL_REQUIRED = "5013",
|
||
|
// Sign Up
|
||
|
USER_ALREADY_EXIST = "5003",
|
||
|
REQUIRED_EMAIL_PASSWORD_SIGN_UP = "5015",
|
||
|
AUTHENTICATION_FAILED_SIGN_UP = "5006",
|
||
|
INVALID_EMAIL_SIGN_UP = "5017",
|
||
|
MAGIC_SIGN_UP_EMAIL_CODE_REQUIRED = "5023",
|
||
|
INVALID_EMAIL_MAGIC_SIGN_UP = "5019",
|
||
|
// Sign In
|
||
|
USER_DOES_NOT_EXIST = "5004",
|
||
|
REQUIRED_EMAIL_PASSWORD_SIGN_IN = "5014",
|
||
|
AUTHENTICATION_FAILED_SIGN_IN = "5005",
|
||
|
INVALID_EMAIL_SIGN_IN = "5016",
|
||
|
MAGIC_SIGN_IN_EMAIL_CODE_REQUIRED = "5022",
|
||
|
INVALID_EMAIL_MAGIC_SIGN_IN = "5018",
|
||
|
// Both Sign in and Sign up
|
||
|
INVALID_MAGIC_CODE = "5008",
|
||
|
EXPIRED_MAGIC_CODE = "5009",
|
||
|
// Oauth
|
||
|
GOOGLE_NOT_CONFIGURED = "5010",
|
||
|
GITHUB_NOT_CONFIGURED = "5011",
|
||
|
GOOGLE_OAUTH_PROVIDER_ERROR = "5021",
|
||
|
GITHUB_OAUTH_PROVIDER_ERROR = "5020",
|
||
|
// Reset Password
|
||
|
INVALID_PASSWORD_TOKEN = "5024",
|
||
|
EXPIRED_PASSWORD_TOKEN = "5025",
|
||
|
// Change password
|
||
|
INCORRECT_OLD_PASSWORD = "5026",
|
||
|
INVALID_NEW_PASSWORD = "5027",
|
||
|
// set password
|
||
|
PASSWORD_ALREADY_SET = "5028", // used in the onboarding and set password page
|
||
|
}
|
||
|
|
||
|
export type TAuthErrorInfo = {
|
||
|
type: EErrorAlertType;
|
||
|
code: EAuthenticationErrorCodes;
|
||
|
title: string;
|
||
|
message: ReactNode;
|
||
|
};
|
||
|
|
||
|
const errorCodeMessages: {
|
||
|
[key in EAuthenticationErrorCodes]: { title: string; message: (email?: string | undefined) => ReactNode };
|
||
|
} = {
|
||
|
// global
|
||
|
[EAuthenticationErrorCodes.INSTANCE_NOT_CONFIGURED]: {
|
||
|
title: `Instance not configured`,
|
||
|
message: () => `Instance not configured. Please contact your administrator.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.SIGNUP_DISABLED]: {
|
||
|
title: `Sign up disabled`,
|
||
|
message: () => `Sign up disabled. Please contact your administrator.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.INVALID_PASSWORD]: {
|
||
|
title: `Invalid password`,
|
||
|
message: () => `Invalid password. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.SMTP_NOT_CONFIGURED]: {
|
||
|
title: `SMTP not configured`,
|
||
|
message: () => `SMTP not configured. Please contact your administrator.`,
|
||
|
},
|
||
|
|
||
|
// email check in both sign up and sign in
|
||
|
[EAuthenticationErrorCodes.INVALID_EMAIL]: {
|
||
|
title: `Invalid email`,
|
||
|
message: () => `Invalid email. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.EMAIL_REQUIRED]: {
|
||
|
title: `Email required`,
|
||
|
message: () => `Email required. Please try again.`,
|
||
|
},
|
||
|
|
||
|
// sign up
|
||
|
[EAuthenticationErrorCodes.USER_ALREADY_EXIST]: {
|
||
|
title: `User already exists`,
|
||
|
message: (email = undefined) => (
|
||
|
<div>
|
||
|
Your account is already registered.
|
||
|
<Link
|
||
|
className="underline underline-offset-4 font-medium hover:font-bold transition-all"
|
||
|
href={`/accounts/sign-in${email ? `?email=${email}` : ``}`}
|
||
|
>
|
||
|
Sign In
|
||
|
</Link>
|
||
|
now.
|
||
|
</div>
|
||
|
),
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.REQUIRED_EMAIL_PASSWORD_SIGN_UP]: {
|
||
|
title: `Email and password required`,
|
||
|
message: () => `Email and password required. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.AUTHENTICATION_FAILED_SIGN_UP]: {
|
||
|
title: `Authentication failed`,
|
||
|
message: () => `Authentication failed. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.INVALID_EMAIL_SIGN_UP]: {
|
||
|
title: `Invalid email`,
|
||
|
message: () => `Invalid email. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.MAGIC_SIGN_UP_EMAIL_CODE_REQUIRED]: {
|
||
|
title: `Email and code required`,
|
||
|
message: () => `Email and code required. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.INVALID_EMAIL_MAGIC_SIGN_UP]: {
|
||
|
title: `Invalid email`,
|
||
|
message: () => `Invalid email. Please try again.`,
|
||
|
},
|
||
|
|
||
|
// sign in
|
||
|
[EAuthenticationErrorCodes.USER_DOES_NOT_EXIST]: {
|
||
|
title: `User does not exist`,
|
||
|
message: (email = undefined) => (
|
||
|
<div>
|
||
|
No account found.
|
||
|
<Link
|
||
|
className="underline underline-offset-4 font-medium hover:font-bold transition-all"
|
||
|
href={`/${email ? `?email=${email}` : ``}`}
|
||
|
>
|
||
|
Create one
|
||
|
</Link>
|
||
|
to get started.
|
||
|
</div>
|
||
|
),
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.REQUIRED_EMAIL_PASSWORD_SIGN_IN]: {
|
||
|
title: `Email and password required`,
|
||
|
message: () => `Email and password required. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.AUTHENTICATION_FAILED_SIGN_IN]: {
|
||
|
title: `Authentication failed`,
|
||
|
message: () => `Authentication failed. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.INVALID_EMAIL_SIGN_IN]: {
|
||
|
title: `Invalid email`,
|
||
|
message: () => `Invalid email. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.MAGIC_SIGN_IN_EMAIL_CODE_REQUIRED]: {
|
||
|
title: `Email and code required`,
|
||
|
message: () => `Email and code required. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.INVALID_EMAIL_MAGIC_SIGN_IN]: {
|
||
|
title: `Invalid email`,
|
||
|
message: () => `Invalid email. Please try again.`,
|
||
|
},
|
||
|
|
||
|
// Both Sign in and Sign up
|
||
|
[EAuthenticationErrorCodes.INVALID_MAGIC_CODE]: {
|
||
|
title: `Authentication failed`,
|
||
|
message: () => `Invalid magic code. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.EXPIRED_MAGIC_CODE]: {
|
||
|
title: `Expired magic code`,
|
||
|
message: () => `Expired magic code. Please try again.`,
|
||
|
},
|
||
|
|
||
|
// Oauth
|
||
|
[EAuthenticationErrorCodes.GOOGLE_NOT_CONFIGURED]: {
|
||
|
title: `Google not configured`,
|
||
|
message: () => `Google not configured. Please contact your administrator.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.GITHUB_NOT_CONFIGURED]: {
|
||
|
title: `GitHub not configured`,
|
||
|
message: () => `GitHub not configured. Please contact your administrator.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.GOOGLE_OAUTH_PROVIDER_ERROR]: {
|
||
|
title: `Google OAuth provider error`,
|
||
|
message: () => `Google OAuth provider error. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.GITHUB_OAUTH_PROVIDER_ERROR]: {
|
||
|
title: `GitHub OAuth provider error`,
|
||
|
message: () => `GitHub OAuth provider error. Please try again.`,
|
||
|
},
|
||
|
|
||
|
// Reset Password
|
||
|
[EAuthenticationErrorCodes.INVALID_PASSWORD_TOKEN]: {
|
||
|
title: `Invalid password token`,
|
||
|
message: () => `Invalid password token. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.EXPIRED_PASSWORD_TOKEN]: {
|
||
|
title: `Expired password token`,
|
||
|
message: () => `Expired password token. Please try again.`,
|
||
|
},
|
||
|
|
||
|
// Change password
|
||
|
[EAuthenticationErrorCodes.INCORRECT_OLD_PASSWORD]: {
|
||
|
title: `Incorrect old password`,
|
||
|
message: () => `Incorrect old password. Please try again.`,
|
||
|
},
|
||
|
[EAuthenticationErrorCodes.INVALID_NEW_PASSWORD]: {
|
||
|
title: `Invalid new password`,
|
||
|
message: () => `Invalid new password. Please try again.`,
|
||
|
},
|
||
|
|
||
|
// set password
|
||
|
[EAuthenticationErrorCodes.PASSWORD_ALREADY_SET]: {
|
||
|
title: `Password already set`,
|
||
|
message: () => `Password already set. Please try again.`,
|
||
|
},
|
||
|
};
|
||
|
|
||
|
export const authErrorHandler = (
|
||
|
errorCode: EAuthenticationErrorCodes,
|
||
|
email?: string | undefined
|
||
|
): TAuthErrorInfo | undefined => {
|
||
|
const toastAlertErrorCodes = [
|
||
|
EAuthenticationErrorCodes.SIGNUP_DISABLED,
|
||
|
EAuthenticationErrorCodes.INVALID_PASSWORD,
|
||
|
EAuthenticationErrorCodes.SMTP_NOT_CONFIGURED,
|
||
|
EAuthenticationErrorCodes.INVALID_EMAIL,
|
||
|
EAuthenticationErrorCodes.EMAIL_REQUIRED,
|
||
|
EAuthenticationErrorCodes.AUTHENTICATION_FAILED_SIGN_UP,
|
||
|
EAuthenticationErrorCodes.INVALID_EMAIL_SIGN_UP,
|
||
|
EAuthenticationErrorCodes.MAGIC_SIGN_UP_EMAIL_CODE_REQUIRED,
|
||
|
EAuthenticationErrorCodes.INVALID_EMAIL_MAGIC_SIGN_UP,
|
||
|
EAuthenticationErrorCodes.AUTHENTICATION_FAILED_SIGN_IN,
|
||
|
EAuthenticationErrorCodes.INVALID_EMAIL_SIGN_IN,
|
||
|
EAuthenticationErrorCodes.INVALID_EMAIL_MAGIC_SIGN_IN,
|
||
|
EAuthenticationErrorCodes.INVALID_MAGIC_CODE,
|
||
|
EAuthenticationErrorCodes.EXPIRED_MAGIC_CODE,
|
||
|
EAuthenticationErrorCodes.GOOGLE_NOT_CONFIGURED,
|
||
|
EAuthenticationErrorCodes.GITHUB_NOT_CONFIGURED,
|
||
|
EAuthenticationErrorCodes.GOOGLE_OAUTH_PROVIDER_ERROR,
|
||
|
EAuthenticationErrorCodes.GITHUB_OAUTH_PROVIDER_ERROR,
|
||
|
EAuthenticationErrorCodes.INVALID_PASSWORD_TOKEN,
|
||
|
EAuthenticationErrorCodes.EXPIRED_PASSWORD_TOKEN,
|
||
|
EAuthenticationErrorCodes.INCORRECT_OLD_PASSWORD,
|
||
|
EAuthenticationErrorCodes.INVALID_NEW_PASSWORD,
|
||
|
EAuthenticationErrorCodes.PASSWORD_ALREADY_SET,
|
||
|
];
|
||
|
const bannerAlertErrorCodes = [
|
||
|
EAuthenticationErrorCodes.INSTANCE_NOT_CONFIGURED,
|
||
|
EAuthenticationErrorCodes.USER_ALREADY_EXIST,
|
||
|
EAuthenticationErrorCodes.USER_DOES_NOT_EXIST,
|
||
|
EAuthenticationErrorCodes.REQUIRED_EMAIL_PASSWORD_SIGN_UP,
|
||
|
EAuthenticationErrorCodes.REQUIRED_EMAIL_PASSWORD_SIGN_IN,
|
||
|
EAuthenticationErrorCodes.MAGIC_SIGN_IN_EMAIL_CODE_REQUIRED,
|
||
|
];
|
||
|
|
||
|
if (toastAlertErrorCodes.includes(errorCode))
|
||
|
return {
|
||
|
type: EErrorAlertType.TOAST_ALERT,
|
||
|
code: errorCode,
|
||
|
title: errorCodeMessages[errorCode]?.title || "Error",
|
||
|
message: errorCodeMessages[errorCode]?.message(email) || "Something went wrong. Please try again.",
|
||
|
};
|
||
|
|
||
|
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;
|
||
|
};
|