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 { EPageTypes } from "@/helpers/authentication.helper"; 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({ 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; 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.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}
); }); SetPasswordPage.getLayout = function getLayout(page: ReactElement) { return ( {page} ); }; export default SetPasswordPage;