import { FormEvent, ReactElement, useEffect, useMemo, useState } from "react"; import { observer } from "mobx-react-lite"; import Image from "next/image"; import { useRouter } from "next/router"; // 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"; import { PageHead } from "@/components/core"; // helpers import { getPasswordStrength } from "@/helpers/password.helper"; // hooks import { useUser } from "@/hooks/store"; // layouts import DefaultLayout from "@/layouts/default-layout"; // lib import { NextPageWithLayout } from "@/lib/types"; // wrappers import { AuthenticationWrapper } from "@/lib/wrappers"; // services import { AuthService } from "@/services/auth.service"; // images import PlaneBackgroundPatternDark from "public/auth/background-pattern-dark.svg"; import PlaneBackgroundPattern from "public/auth/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); // hooks const { resolvedTheme } = useTheme(); const { data: user } = useUser(); 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); } catch (err: any) { setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: err?.error ?? "Something went wrong. Please try again.", }); } }; 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 && }
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;