/** * Copyright (c) 2023-present Plane Software, Inc. and contributors * SPDX-License-Identifier: AGPL-3.0-only * See the LICENSE file for details. */ import React, { useEffect, useMemo, useRef, useState } from "react"; import { observer } from "mobx-react"; import { Eye, EyeOff, XCircle } from "lucide-react"; // plane imports import { API_BASE_URL, E_PASSWORD_STRENGTH } from "@plane/constants"; import { Button } from "@plane/propel/button"; import { AuthService } from "@plane/services"; import { Input, Spinner, PasswordStrengthIndicator } from "@plane/ui"; import { getPasswordStrength } from "@plane/utils"; // types import { EAuthModes, EAuthSteps } from "@/types/auth"; type Props = { email: string; isPasswordAutoset: boolean; isSMTPConfigured: boolean; mode: EAuthModes; nextPath: string | undefined; handleEmailClear: () => void; handleAuthStep: (step: EAuthSteps) => void; }; type TPasswordFormValues = { email: string; password: string; confirm_password?: string; }; const defaultValues: TPasswordFormValues = { email: "", password: "", }; const authService = new AuthService(); export const AuthPasswordForm = observer(function AuthPasswordForm(props: Props) { const { email, nextPath, isSMTPConfigured, handleAuthStep, handleEmailClear, mode } = props; // ref const formRef = useRef(null); // states const [csrfPromise, setCsrfPromise] = useState | undefined>(undefined); const [passwordFormData, setPasswordFormData] = useState({ ...defaultValues, email }); const [showPassword, setShowPassword] = useState({ password: false, retypePassword: false, }); const [isSubmitting, setIsSubmitting] = useState(false); const [isPasswordInputFocused, setIsPasswordInputFocused] = useState(false); const [isRetryPasswordInputFocused, setIsRetryPasswordInputFocused] = useState(false); const handleShowPassword = (key: keyof typeof showPassword) => setShowPassword((prev) => ({ ...prev, [key]: !prev[key] })); const handleFormChange = (key: keyof TPasswordFormValues, value: string) => setPasswordFormData((prev) => ({ ...prev, [key]: value })); useEffect(() => { if (csrfPromise === undefined) { const promise = authService.requestCSRFToken(); setCsrfPromise(promise); } }, [csrfPromise]); const redirectToUniqueCodeSignIn = async () => { handleAuthStep(EAuthSteps.UNIQUE_CODE); }; const passwordSupport = passwordFormData.password.length > 0 && mode === EAuthModes.SIGN_UP && getPasswordStrength(passwordFormData.password) != E_PASSWORD_STRENGTH.STRENGTH_VALID && ( ); const isButtonDisabled = useMemo( () => !isSubmitting && !!passwordFormData.password && (mode === EAuthModes.SIGN_UP ? getPasswordStrength(passwordFormData.password) === E_PASSWORD_STRENGTH.STRENGTH_VALID && passwordFormData.password === passwordFormData.confirm_password : true) ? false : true, [isSubmitting, mode, passwordFormData.confirm_password, passwordFormData.password] ); const password = passwordFormData.password ?? ""; const confirmPassword = passwordFormData.confirm_password ?? ""; const renderPasswordMatchError = !isRetryPasswordInputFocused || confirmPassword.length >= password.length; const handleCSRFToken = async () => { if (!formRef || !formRef.current) return; const token = await csrfPromise; if (!token?.csrf_token) return; const csrfElement = formRef.current.querySelector("input[name=csrfmiddlewaretoken]"); csrfElement?.setAttribute("value", token?.csrf_token); }; return (
{ event.preventDefault(); await handleCSRFToken(); if (formRef.current) { formRef.current.submit(); } setIsSubmitting(true); }} onError={() => setIsSubmitting(false)} >
handleFormChange("email", e.target.value)} placeholder="name@company.com" className={`h-10 w-full border-0 disable-autofill-style placeholder:text-placeholder`} disabled /> {passwordFormData.email.length > 0 && ( )}
handleFormChange("password", e.target.value)} placeholder="Enter password" className="h-10 w-full border border-subtle !bg-surface-1 pr-12 disable-autofill-style placeholder:text-placeholder" onFocus={() => setIsPasswordInputFocused(true)} onBlur={() => setIsPasswordInputFocused(false)} autoComplete="off" autoFocus /> {showPassword?.password ? ( handleShowPassword("password")} /> ) : ( handleShowPassword("password")} /> )}
{passwordSupport}
{mode === EAuthModes.SIGN_UP && (
handleFormChange("confirm_password", e.target.value)} placeholder="Confirm password" className="h-10 w-full border border-subtle !bg-surface-1 pr-12 disable-autofill-style placeholder:text-placeholder" onFocus={() => setIsRetryPasswordInputFocused(true)} onBlur={() => setIsRetryPasswordInputFocused(false)} autoComplete="off" /> {showPassword?.retypePassword ? ( handleShowPassword("retypePassword")} /> ) : ( handleShowPassword("retypePassword")} /> )}
{!!passwordFormData.confirm_password && passwordFormData.password !== passwordFormData.confirm_password && renderPasswordMatchError && Passwords don{"'"}t match}
)}
{mode === EAuthModes.SIGN_IN ? ( <> {isSMTPConfigured && ( )} ) : ( )}
); });