2024-05-08 06:55:36 +00:00
|
|
|
import React, { FC, useEffect, useState } from "react";
|
2024-04-29 06:42:33 +00:00
|
|
|
import { observer } from "mobx-react";
|
|
|
|
import { useRouter } from "next/router";
|
2024-04-30 14:00:42 +00:00
|
|
|
import { IEmailCheckData } from "@plane/types";
|
2024-05-02 13:08:14 +00:00
|
|
|
import { TOAST_TYPE, setToast } from "@plane/ui";
|
2024-04-29 06:42:33 +00:00
|
|
|
// components
|
|
|
|
import {
|
2024-04-30 14:00:42 +00:00
|
|
|
AuthHeader,
|
|
|
|
AuthBanner,
|
2024-04-29 06:42:33 +00:00
|
|
|
AuthEmailForm,
|
|
|
|
AuthPasswordForm,
|
|
|
|
OAuthOptions,
|
|
|
|
TermsAndConditions,
|
2024-05-02 07:44:02 +00:00
|
|
|
AuthUniqueCodeForm,
|
2024-04-29 06:42:33 +00:00
|
|
|
} from "@/components/account";
|
2024-04-30 14:00:42 +00:00
|
|
|
// helpers
|
|
|
|
import {
|
|
|
|
EAuthModes,
|
|
|
|
EAuthSteps,
|
|
|
|
EAuthenticationErrorCodes,
|
|
|
|
EErrorAlertType,
|
|
|
|
TAuthErrorInfo,
|
|
|
|
authErrorHandler,
|
|
|
|
} from "@/helpers/authentication.helper";
|
|
|
|
// hooks
|
2024-04-29 06:42:33 +00:00
|
|
|
import { useInstance } from "@/hooks/store";
|
|
|
|
// services
|
|
|
|
import { AuthService } from "@/services/auth.service";
|
|
|
|
|
|
|
|
const authService = new AuthService();
|
|
|
|
|
2024-05-08 06:55:36 +00:00
|
|
|
type TAuthRoot = {
|
|
|
|
authMode: EAuthModes;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AuthRoot: FC<TAuthRoot> = observer((props) => {
|
2024-04-29 06:42:33 +00:00
|
|
|
//router
|
|
|
|
const router = useRouter();
|
2024-05-08 06:55:36 +00:00
|
|
|
const { email: emailParam, invitation_id, slug: workspaceSlug, error_code } = router.query;
|
|
|
|
// props
|
|
|
|
const { authMode } = props;
|
2024-04-29 06:42:33 +00:00
|
|
|
// states
|
|
|
|
const [authStep, setAuthStep] = useState<EAuthSteps>(EAuthSteps.EMAIL);
|
|
|
|
const [email, setEmail] = useState(emailParam ? emailParam.toString() : "");
|
2024-04-30 14:00:42 +00:00
|
|
|
const [errorInfo, setErrorInfo] = useState<TAuthErrorInfo | undefined>(undefined);
|
2024-04-29 06:42:33 +00:00
|
|
|
// hooks
|
|
|
|
const { instance } = useInstance();
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-05-08 06:55:36 +00:00
|
|
|
if (error_code) {
|
|
|
|
const errorhandler = authErrorHandler(error_code?.toString() as EAuthenticationErrorCodes);
|
2024-05-06 12:49:24 +00:00
|
|
|
if (errorhandler) {
|
2024-05-08 06:55:36 +00:00
|
|
|
if (
|
|
|
|
[
|
|
|
|
EAuthenticationErrorCodes.AUTHENTICATION_FAILED_SIGN_IN,
|
|
|
|
EAuthenticationErrorCodes.AUTHENTICATION_FAILED_SIGN_UP,
|
|
|
|
].includes(errorhandler.code)
|
|
|
|
)
|
|
|
|
setAuthStep(EAuthSteps.PASSWORD);
|
|
|
|
if (
|
|
|
|
[EAuthenticationErrorCodes.INVALID_MAGIC_CODE, EAuthenticationErrorCodes.EXPIRED_MAGIC_CODE].includes(
|
|
|
|
errorhandler.code
|
|
|
|
)
|
|
|
|
)
|
|
|
|
setAuthStep(EAuthSteps.UNIQUE_CODE);
|
|
|
|
|
|
|
|
// validating wheather to show alert to banner
|
2024-05-06 12:49:24 +00:00
|
|
|
if (errorhandler?.type === EErrorAlertType.TOAST_ALERT) {
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
|
|
|
title: errorhandler?.title,
|
|
|
|
message: errorhandler?.message as string,
|
|
|
|
});
|
|
|
|
} else setErrorInfo(errorhandler);
|
|
|
|
}
|
2024-04-29 06:42:33 +00:00
|
|
|
}
|
2024-05-08 06:55:36 +00:00
|
|
|
}, [error_code, authMode]);
|
2024-04-29 06:42:33 +00:00
|
|
|
|
|
|
|
// step 1 submit handler- email verification
|
|
|
|
const handleEmailVerification = async (data: IEmailCheckData) => {
|
|
|
|
setEmail(data.email);
|
|
|
|
|
2024-05-08 06:55:36 +00:00
|
|
|
const emailCheckRequest =
|
|
|
|
authMode === EAuthModes.SIGN_IN ? authService.signInEmailCheck(data) : authService.signUpEmailCheck(data);
|
|
|
|
|
|
|
|
await emailCheckRequest
|
2024-04-30 14:00:42 +00:00
|
|
|
.then(() => {
|
|
|
|
setAuthStep(EAuthSteps.PASSWORD);
|
2024-04-29 06:42:33 +00:00
|
|
|
})
|
2024-05-08 06:55:36 +00:00
|
|
|
.catch((error) => {
|
|
|
|
const errorhandler = authErrorHandler(error?.error_code.toString());
|
|
|
|
if (errorhandler?.type === EErrorAlertType.BANNER_ALERT) {
|
|
|
|
setErrorInfo(errorhandler);
|
|
|
|
return;
|
|
|
|
} else if (errorhandler?.type === EErrorAlertType.TOAST_ALERT)
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
|
|
|
title: errorhandler?.title,
|
|
|
|
message: (errorhandler?.message as string) || "Something went wrong. Please try again.",
|
|
|
|
});
|
2024-04-29 06:42:33 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const isOAuthEnabled =
|
|
|
|
instance?.config && (instance?.config?.is_google_enabled || instance?.config?.is_github_enabled);
|
|
|
|
|
|
|
|
return (
|
2024-05-02 07:44:02 +00:00
|
|
|
<div className="relative flex flex-col space-y-6">
|
|
|
|
<AuthHeader
|
|
|
|
workspaceSlug={workspaceSlug?.toString() || undefined}
|
|
|
|
invitationId={invitation_id?.toString() || undefined}
|
|
|
|
invitationEmail={email || undefined}
|
2024-05-08 06:55:36 +00:00
|
|
|
authMode={authMode}
|
2024-05-02 07:44:02 +00:00
|
|
|
currentAuthStep={authStep}
|
2024-05-02 13:08:14 +00:00
|
|
|
>
|
|
|
|
{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
|
|
|
|
email={email}
|
|
|
|
handleEmailClear={() => {
|
|
|
|
setEmail("");
|
|
|
|
setAuthStep(EAuthSteps.EMAIL);
|
|
|
|
}}
|
|
|
|
submitButtonText="Continue"
|
|
|
|
mode={authMode}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{authStep === EAuthSteps.PASSWORD && (
|
|
|
|
<AuthPasswordForm
|
|
|
|
email={email}
|
|
|
|
handleEmailClear={() => {
|
|
|
|
setEmail("");
|
|
|
|
setAuthStep(EAuthSteps.EMAIL);
|
|
|
|
}}
|
|
|
|
handleStepChange={(step) => setAuthStep(step)}
|
|
|
|
mode={authMode}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{isOAuthEnabled && <OAuthOptions />}
|
|
|
|
<TermsAndConditions isSignUp={false} />
|
|
|
|
</AuthHeader>
|
2024-05-02 07:44:02 +00:00
|
|
|
</div>
|
2024-04-29 06:42:33 +00:00
|
|
|
);
|
|
|
|
});
|