2024-05-08 17:31:20 +00:00
|
|
|
import React, { FC, useEffect, useState } from "react";
|
|
|
|
import { observer } from "mobx-react";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { IEmailCheckData } from "@plane/types";
|
|
|
|
// components
|
|
|
|
import {
|
|
|
|
AuthHeader,
|
|
|
|
AuthBanner,
|
|
|
|
AuthEmailForm,
|
|
|
|
AuthPasswordForm,
|
|
|
|
OAuthOptions,
|
|
|
|
TermsAndConditions,
|
|
|
|
AuthUniqueCodeForm,
|
|
|
|
} from "@/components/account";
|
|
|
|
// helpers
|
|
|
|
import {
|
|
|
|
EAuthModes,
|
|
|
|
EAuthSteps,
|
|
|
|
EAuthenticationErrorCodes,
|
|
|
|
EErrorAlertType,
|
|
|
|
TAuthErrorInfo,
|
|
|
|
authErrorHandler,
|
|
|
|
} from "@/helpers/authentication.helper";
|
|
|
|
// hooks
|
|
|
|
import { useInstance } from "@/hooks/store";
|
|
|
|
// services
|
|
|
|
import { AuthService } from "@/services/auth.service";
|
|
|
|
|
|
|
|
const authService = new AuthService();
|
|
|
|
|
|
|
|
type TAuthRoot = {
|
|
|
|
authMode: EAuthModes;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AuthRoot: FC<TAuthRoot> = observer((props) => {
|
|
|
|
//router
|
|
|
|
const router = useRouter();
|
|
|
|
const { email: emailParam, invitation_id, slug: workspaceSlug, error_code } = router.query;
|
|
|
|
// props
|
|
|
|
const { authMode } = props;
|
|
|
|
// states
|
|
|
|
const [authStep, setAuthStep] = useState<EAuthSteps>(EAuthSteps.EMAIL);
|
|
|
|
const [email, setEmail] = useState(emailParam ? emailParam.toString() : "");
|
|
|
|
const [errorInfo, setErrorInfo] = useState<TAuthErrorInfo | undefined>(undefined);
|
2024-05-10 12:00:38 +00:00
|
|
|
const [isPasswordAutoset, setIsPasswordAutoset] = useState(true);
|
2024-05-08 17:31:20 +00:00
|
|
|
// hooks
|
2024-05-18 10:52:53 +00:00
|
|
|
const { config } = useInstance();
|
2024-05-08 17:31:20 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (error_code) {
|
|
|
|
const errorhandler = authErrorHandler(error_code?.toString() as EAuthenticationErrorCodes);
|
|
|
|
if (errorhandler) {
|
|
|
|
if (
|
|
|
|
[
|
|
|
|
EAuthenticationErrorCodes.AUTHENTICATION_FAILED_SIGN_IN,
|
|
|
|
EAuthenticationErrorCodes.AUTHENTICATION_FAILED_SIGN_UP,
|
|
|
|
].includes(errorhandler.code)
|
|
|
|
)
|
|
|
|
setAuthStep(EAuthSteps.PASSWORD);
|
|
|
|
if (
|
2024-05-20 06:53:48 +00:00
|
|
|
[
|
|
|
|
EAuthenticationErrorCodes.INVALID_EMAIL_MAGIC_SIGN_IN,
|
|
|
|
EAuthenticationErrorCodes.INVALID_EMAIL_MAGIC_SIGN_UP,
|
|
|
|
EAuthenticationErrorCodes.EXPIRED_MAGIC_CODE_SIGN_IN,
|
|
|
|
EAuthenticationErrorCodes.EXPIRED_MAGIC_CODE_SIGN_UP,
|
|
|
|
EAuthenticationErrorCodes.EMAIL_CODE_ATTEMPT_EXHAUSTED_SIGN_IN,
|
|
|
|
EAuthenticationErrorCodes.EMAIL_CODE_ATTEMPT_EXHAUSTED_SIGN_UP,
|
|
|
|
].includes(errorhandler.code)
|
2024-05-08 17:31:20 +00:00
|
|
|
)
|
|
|
|
setAuthStep(EAuthSteps.UNIQUE_CODE);
|
2024-05-10 12:00:38 +00:00
|
|
|
setErrorInfo(errorhandler);
|
2024-05-08 17:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [error_code, authMode]);
|
|
|
|
|
2024-05-18 10:52:53 +00:00
|
|
|
const isSMTPConfigured = config?.is_smtp_configured || false;
|
|
|
|
const isMagicLoginEnabled = config?.is_magic_login_enabled || false;
|
|
|
|
const isEmailPasswordEnabled = config?.is_email_password_enabled || false;
|
2024-05-14 15:23:51 +00:00
|
|
|
|
2024-05-10 12:00:38 +00:00
|
|
|
// submit handler- email verification
|
2024-05-08 17:31:20 +00:00
|
|
|
const handleEmailVerification = async (data: IEmailCheckData) => {
|
|
|
|
setEmail(data.email);
|
|
|
|
const emailCheckRequest =
|
|
|
|
authMode === EAuthModes.SIGN_IN ? authService.signInEmailCheck(data) : authService.signUpEmailCheck(data);
|
|
|
|
|
|
|
|
await emailCheckRequest
|
2024-05-10 12:00:38 +00:00
|
|
|
.then(async (response) => {
|
2024-05-08 17:31:20 +00:00
|
|
|
if (authMode === EAuthModes.SIGN_IN) {
|
2024-05-10 12:00:38 +00:00
|
|
|
if (response.is_password_autoset) {
|
|
|
|
setAuthStep(EAuthSteps.UNIQUE_CODE);
|
|
|
|
generateEmailUniqueCode(data.email);
|
2024-05-14 15:23:51 +00:00
|
|
|
} else if (isEmailPasswordEnabled) {
|
2024-05-10 12:00:38 +00:00
|
|
|
setIsPasswordAutoset(false);
|
|
|
|
setAuthStep(EAuthSteps.PASSWORD);
|
|
|
|
}
|
2024-05-09 12:16:31 +00:00
|
|
|
} else {
|
2024-05-14 15:23:51 +00:00
|
|
|
if (isSMTPConfigured && isMagicLoginEnabled) {
|
2024-05-10 12:00:38 +00:00
|
|
|
setAuthStep(EAuthSteps.UNIQUE_CODE);
|
|
|
|
generateEmailUniqueCode(data.email);
|
2024-05-14 15:23:51 +00:00
|
|
|
} else if (isEmailPasswordEnabled) {
|
|
|
|
setAuthStep(EAuthSteps.PASSWORD);
|
|
|
|
}
|
2024-05-09 12:16:31 +00:00
|
|
|
}
|
2024-05-08 17:31:20 +00:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2024-05-14 15:23:51 +00:00
|
|
|
const errorhandler = authErrorHandler(error?.error_code?.toString(), data?.email || undefined);
|
2024-05-10 12:00:38 +00:00
|
|
|
if (errorhandler?.type) setErrorInfo(errorhandler);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// generating the unique code
|
|
|
|
const generateEmailUniqueCode = async (email: string): Promise<{ code: string } | undefined> => {
|
|
|
|
const payload = { email: email };
|
|
|
|
return await authService
|
|
|
|
.generateUniqueCode(payload)
|
|
|
|
.then(() => ({ code: "" }))
|
|
|
|
.catch((error) => {
|
|
|
|
const errorhandler = authErrorHandler(error?.error_code.toString());
|
|
|
|
if (errorhandler?.type) setErrorInfo(errorhandler);
|
|
|
|
throw error;
|
2024-05-08 17:31:20 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="relative flex flex-col space-y-6">
|
|
|
|
<AuthHeader
|
|
|
|
workspaceSlug={workspaceSlug?.toString() || undefined}
|
|
|
|
invitationId={invitation_id?.toString() || undefined}
|
|
|
|
invitationEmail={email || undefined}
|
|
|
|
authMode={authMode}
|
|
|
|
currentAuthStep={authStep}
|
|
|
|
>
|
|
|
|
{errorInfo && errorInfo?.type === EErrorAlertType.BANNER_ALERT && (
|
|
|
|
<AuthBanner bannerData={errorInfo} handleBannerData={(value) => setErrorInfo(value)} />
|
|
|
|
)}
|
|
|
|
{authStep === EAuthSteps.EMAIL && <AuthEmailForm defaultEmail={email} onSubmit={handleEmailVerification} />}
|
|
|
|
{authStep === EAuthSteps.UNIQUE_CODE && (
|
|
|
|
<AuthUniqueCodeForm
|
2024-05-10 12:00:38 +00:00
|
|
|
mode={authMode}
|
2024-05-08 17:31:20 +00:00
|
|
|
email={email}
|
|
|
|
handleEmailClear={() => {
|
|
|
|
setEmail("");
|
|
|
|
setAuthStep(EAuthSteps.EMAIL);
|
|
|
|
}}
|
2024-05-10 12:00:38 +00:00
|
|
|
generateEmailUniqueCode={generateEmailUniqueCode}
|
2024-05-08 17:31:20 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{authStep === EAuthSteps.PASSWORD && (
|
|
|
|
<AuthPasswordForm
|
2024-05-10 12:00:38 +00:00
|
|
|
mode={authMode}
|
|
|
|
isPasswordAutoset={isPasswordAutoset}
|
|
|
|
isSMTPConfigured={isSMTPConfigured}
|
2024-05-08 17:31:20 +00:00
|
|
|
email={email}
|
|
|
|
handleEmailClear={() => {
|
|
|
|
setEmail("");
|
|
|
|
setAuthStep(EAuthSteps.EMAIL);
|
|
|
|
}}
|
2024-05-10 12:00:38 +00:00
|
|
|
handleAuthStep={(step: EAuthSteps) => {
|
|
|
|
if (step === EAuthSteps.UNIQUE_CODE) generateEmailUniqueCode(email);
|
|
|
|
setAuthStep(step);
|
|
|
|
}}
|
2024-05-08 17:31:20 +00:00
|
|
|
/>
|
|
|
|
)}
|
2024-05-16 14:06:09 +00:00
|
|
|
<OAuthOptions isSignUp={authMode === EAuthModes.SIGN_UP} />
|
|
|
|
<TermsAndConditions isSignUp={authMode === EAuthModes.SIGN_UP} />
|
2024-05-08 17:31:20 +00:00
|
|
|
</AuthHeader>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|