"use client"; import { FormEvent, useEffect, useMemo, useState } from "react"; import { observer } from "mobx-react-lite"; import Image from "next/image"; import Link from "next/link"; import { useRouter, useSearchParams } from "next/navigation"; // icons import { useTheme } from "next-themes"; import { Eye, EyeOff } from "lucide-react"; // ui import { Button, Input, TOAST_TYPE, setToast } from "@plane/ui"; // components import { PasswordStrengthMeter } from "@/components/account"; // helpers import { EPageTypes } from "@/helpers/authentication.helper"; import { getPasswordStrength } from "@/helpers/password.helper"; // hooks import { useUser } from "@/hooks/store"; // wrappers import { AuthenticationWrapper } from "@/lib/wrappers"; // services // images import PlaneBackgroundPatternDark from "@/public/auth/background-pattern-dark.svg"; import PlaneBackgroundPattern from "@/public/auth/background-pattern.svg"; import BlackHorizontalLogo from "@/public/plane-logos/black-horizontal-with-blue-logo.png"; import WhiteHorizontalLogo from "@/public/plane-logos/white-horizontal-with-blue-logo.png"; import { AuthService } from "@/services/auth.service"; type TResetPasswordFormValues = { email: string; password: string; confirm_password?: string; }; const defaultValues: TResetPasswordFormValues = { email: "", password: "", }; // services const authService = new AuthService(); const SetPasswordPage = observer(() => { // router const router = useRouter(); // search params const searchParams = useSearchParams(); const email = searchParams.get("email"); // states const [showPassword, setShowPassword] = useState({ password: false, retypePassword: false, }); const [passwordFormData, setPasswordFormData] = useState({ ...defaultValues, email: email ? email.toString() : "", }); const [csrfToken, setCsrfToken] = useState(undefined); const [isPasswordInputFocused, setIsPasswordInputFocused] = useState(false); const [isRetryPasswordInputFocused, setIsRetryPasswordInputFocused] = useState(false); // hooks const { resolvedTheme } = useTheme(); // hooks const { data: user, handleSetPassword } = useUser(); useEffect(() => { if (csrfToken === undefined) authService.requestCSRFToken().then((data) => data?.csrf_token && setCsrfToken(data.csrf_token)); }, [csrfToken]); const handleShowPassword = (key: keyof typeof showPassword) => setShowPassword((prev) => ({ ...prev, [key]: !prev[key] })); 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 handleSubmit = async (e: FormEvent) => { try { e.preventDefault(); if (!csrfToken) throw new Error("csrf token not found"); await handleSetPassword(csrfToken, { password: passwordFormData.password }); router.push("/"); } catch (err: any) { setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: err?.error ?? "Something went wrong. Please try again.", }); } }; const password = passwordFormData?.password ?? ""; const confirmPassword = passwordFormData?.confirm_password ?? ""; const renderPasswordMatchError = !isRetryPasswordInputFocused || confirmPassword.length >= password.length; const logo = resolvedTheme === "light" ? BlackHorizontalLogo : WhiteHorizontalLogo; return (
Plane background pattern
Plane logo

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.password ? ( handleShowPassword("password")} /> ) : ( handleShowPassword("password")} /> )}
{isPasswordInputFocused && }
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" onFocus={() => setIsRetryPasswordInputFocused(true)} onBlur={() => setIsRetryPasswordInputFocused(false)} /> {showPassword.retypePassword ? ( handleShowPassword("retypePassword")} /> ) : ( handleShowPassword("retypePassword")} /> )}
{!!passwordFormData.confirm_password && passwordFormData.password !== passwordFormData.confirm_password && renderPasswordMatchError && Passwords don{"'"}t match}
); }); export default SetPasswordPage;