import { FormEvent, ReactElement, useEffect, useMemo, useState } from "react"; import { observer } from "mobx-react-lite"; import Image from "next/image"; import { useRouter } from "next/router"; import useSWR from "swr"; // icons import { Eye, EyeOff } from "lucide-react"; // ui import { Button, Input, Spinner, TOAST_TYPE, setToast } from "@plane/ui"; // components import { PasswordStrengthMeter } from "@/components/account"; import { PageHead } from "@/components/core"; // helpers import { getPasswordStrength } from "@/helpers/password.helper"; // hooks import { useUser } from "@/hooks/store"; import useAuthRedirection from "@/hooks/use-auth-redirection"; // layouts import { UserAuthWrapper } from "@/layouts/auth-layout"; import DefaultLayout from "@/layouts/default-layout"; // lib import { NextPageWithLayout } from "@/lib/types"; // services import { AuthService } from "@/services/auth.service"; // images import PlaneBackgroundPattern from "public/onboarding/background-pattern.svg"; import BluePlaneLogoWithoutText from "public/plane-logos/blue-without-text.png"; type TResetPasswordFormValues = { email: string; password: string; confirm_password?: string; }; const defaultValues: TResetPasswordFormValues = { email: "", password: "", }; // services const authService = new AuthService(); const SetPasswordPage: NextPageWithLayout = observer(() => { // router const router = useRouter(); const { email } = router.query; // states const [showPassword, setShowPassword] = useState(false); const [passwordFormData, setPasswordFormData] = useState({ ...defaultValues, email: email ? email.toString() : "", }); const [csrfToken, setCsrfToken] = useState(undefined); const [isPasswordInputFocused, setIsPasswordInputFocused] = useState(false); const { data: user, fetchCurrentUser } = useUser(); // store hooks //const { captureEvent } = useEventTracker(); // sign in redirection hook const { isRedirecting, handleRedirection } = useAuthRedirection(); const { isLoading } = useSWR(`CURRENT_USER_DETAILS`, () => fetchCurrentUser(), { shouldRetryOnError: false, }); useEffect(() => { if (user && !user?.is_password_autoset) handleRedirection(); }, [handleRedirection, user?.is_password_autoset]); useEffect(() => { if (csrfToken === undefined) authService.requestCSRFToken().then((data) => data?.csrf_token && setCsrfToken(data.csrf_token)); }, [csrfToken]); const handleFormChange = (key: keyof TResetPasswordFormValues, value: string) => setPasswordFormData((prev) => ({ ...prev, [key]: value })); const isButtonDisabled = useMemo( () => !!passwordFormData.password && getPasswordStrength(passwordFormData.password) >= 3 && passwordFormData.password === passwordFormData.confirm_password ? false : true, [passwordFormData] ); const handleSetPassword = async (password: string) => { if (!csrfToken) throw new Error("csrf token not found"); await authService.setPassword(csrfToken, { password }); }; const handleSubmit = async (e: FormEvent) => { try { e.preventDefault(); await handleSetPassword(passwordFormData.password); await handleRedirection(); } catch (err: any) { setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: err?.error ?? "Something went wrong. Please try again.", }); } }; if (isRedirecting || isLoading) return (
); return (
Plane background pattern
Plane Logo Plane

Secure your account

Setting password helps you login securely

handleSubmit(e)}>
handleFormChange("password", e.target.value)} //hasError={Boolean(errors.password)} placeholder="Enter password" className="h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 placeholder:text-onboarding-text-400" minLength={8} onFocus={() => setIsPasswordInputFocused(true)} onBlur={() => setIsPasswordInputFocused(false)} autoFocus /> {showPassword ? ( setShowPassword(false)} /> ) : ( setShowPassword(true)} /> )}
{isPasswordInputFocused && }
{getPasswordStrength(passwordFormData.password) >= 3 && (
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 )}
)}
); }); SetPasswordPage.getLayout = function getLayout(page: ReactElement) { return ( {page} ); }; export default SetPasswordPage;