import React, { useEffect, useMemo, useState } from "react"; // icons import Link from "next/link"; import { useRouter } from "next/router"; import { Eye, EyeOff, XCircle } from "lucide-react"; // ui import { Button, Input, Spinner } from "@plane/ui"; import { EAuthModes, EAuthSteps, ForgotPasswordPopover, PasswordStrengthMeter } from "@/components/accounts"; // helpers import { API_BASE_URL } from "@/helpers/common.helper"; // services import { getPasswordStrength } from "@/helpers/password.helper"; // hooks import { useInstance } from "@/hooks/store"; import { AuthService } from "@/services/authentication.service"; type Props = { email: string; mode: EAuthModes; handleEmailClear: () => void; handleStepChange: (step: EAuthSteps) => void; }; type TPasswordFormValues = { email: string; password: string; confirm_password?: string; }; const defaultValues: TPasswordFormValues = { email: "", password: "", }; const authService = new AuthService(); export const PasswordForm: React.FC = (props) => { const { email, mode, handleEmailClear, handleStepChange } = props; // states const [passwordFormData, setPasswordFormData] = useState({ ...defaultValues, email }); const [showPassword, setShowPassword] = useState(false); const [csrfToken, setCsrfToken] = useState(undefined); const [isPasswordInputFocused, setIsPasswordInputFocused] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false); // hooks const { instance } = useInstance(); // router const router = useRouter(); const { next_path } = router.query; // derived values const isSmtpConfigured = instance?.config?.is_smtp_configured; const handleFormChange = (key: keyof TPasswordFormValues, value: string) => setPasswordFormData((prev) => ({ ...prev, [key]: value })); useEffect(() => { if (csrfToken === undefined) authService.requestCSRFToken().then((data) => data?.csrf_token && setCsrfToken(data.csrf_token)); }, [csrfToken]); const redirectToUniqueCodeLogin = () => { handleStepChange(EAuthSteps.UNIQUE_CODE); }; const passwordSupport = mode === EAuthModes.SIGN_IN ? (
{isSmtpConfigured ? ( Forgot your password? ) : ( )}
) : ( isPasswordInputFocused && ); const isButtonDisabled = useMemo( () => !isSubmitting && !!passwordFormData.password && (mode === EAuthModes.SIGN_UP ? getPasswordStrength(passwordFormData.password) >= 3 && passwordFormData.password === passwordFormData.confirm_password : true) ? false : true, [isSubmitting, mode, passwordFormData.confirm_password, passwordFormData.password] ); return (
setIsSubmitting(true)} onError={() => setIsSubmitting(false)} >
handleFormChange("email", e.target.value)} // hasError={Boolean(errors.email)} placeholder="name@company.com" className="h-[46px] w-full border border-onboarding-border-100 pr-12 placeholder:text-onboarding-text-400" /> {passwordFormData.email.length > 0 && ( )}
handleFormChange("password", e.target.value)} placeholder="Enter password" className="h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 placeholder:text-onboarding-text-400" onFocus={() => setIsPasswordInputFocused(true)} onBlur={() => setIsPasswordInputFocused(false)} autoFocus /> {showPassword ? ( setShowPassword(false)} /> ) : ( setShowPassword(true)} /> )}
{passwordSupport}
{mode === EAuthModes.SIGN_UP && (
handleFormChange("confirm_password", e.target.value)} placeholder="Confirm password" className="h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 placeholder:text-onboarding-text-400" /> {showPassword ? ( setShowPassword(false)} /> ) : ( setShowPassword(true)} /> )}
{!!passwordFormData.confirm_password && passwordFormData.password !== passwordFormData.confirm_password && ( Passwords don{"'"}t match )}
)}
{mode === EAuthModes.SIGN_IN ? ( <> {instance && isSmtpConfigured && ( )} ) : ( )}
); };