import { ReactElement, useEffect, useState } from "react"; import { observer } from "mobx-react"; import { useRouter } from "next/router"; import { Controller, useForm } from "react-hook-form"; import { Eye, EyeOff } from "lucide-react"; // ui import { Button, Input, TOAST_TYPE, setToast } from "@plane/ui"; // components import { PasswordStrengthMeter } from "@/components/account"; import { LogoSpinner } from "@/components/common"; import { PageHead } from "@/components/core"; import { SidebarHamburgerToggle } from "@/components/core/sidebar"; // helpers import { authErrorHandler } from "@/helpers/authentication.helper"; import { getPasswordStrength } from "@/helpers/password.helper"; // hooks import { useAppTheme, useUser } from "@/hooks/store"; // layout import { ProfileSettingsLayout } from "@/layouts/settings-layout"; // types import { NextPageWithLayout } from "@/lib/types"; // services import { AuthService } from "@/services/auth.service"; import { UserService } from "@/services/user.service"; export interface FormValues { old_password: string; new_password: string; confirm_password: string; } const defaultValues: FormValues = { old_password: "", new_password: "", confirm_password: "", }; export const userService = new UserService(); export const authService = new AuthService(); const defaultShowPassword = { oldPassword: false, password: false, confirmPassword: false, }; const ChangePasswordPage: NextPageWithLayout = observer(() => { // states const [isPageLoading, setIsPageLoading] = useState(true); const [showPassword, setShowPassword] = useState(defaultShowPassword); const [isPasswordInputFocused, setIsPasswordInputFocused] = useState(false); const [isRetryPasswordInputFocused, setIsRetryPasswordInputFocused] = useState(false); // router const router = useRouter(); // store hooks const { toggleSidebar } = useAppTheme(); const { data: currentUser } = useUser(); // use form const { control, handleSubmit, watch, formState: { errors, isSubmitting }, reset, } = useForm({ defaultValues }); // derived values const oldPassword = watch("old_password"); const password = watch("new_password"); const confirmPassword = watch("confirm_password"); const isNewPasswordSameAsOldPassword = oldPassword !== "" && password !== "" && password === oldPassword; const handleShowPassword = (key: keyof typeof showPassword) => setShowPassword((prev) => ({ ...prev, [key]: !prev[key] })); const handleChangePassword = async (formData: FormValues) => { const { old_password, new_password } = formData; try { const csrfToken = await authService.requestCSRFToken().then((data) => data?.csrf_token); if (!csrfToken) throw new Error("csrf token not found"); await userService.changePassword(csrfToken, { old_password, new_password, }); reset(defaultValues); setShowPassword(defaultShowPassword); setToast({ type: TOAST_TYPE.SUCCESS, title: "Success!", message: "Password changed successfully.", }); } catch (err: any) { const errorInfo = authErrorHandler(err.error_code?.toString()); setToast({ type: TOAST_TYPE.ERROR, title: errorInfo?.title ?? "Error!", message: typeof errorInfo?.message === "string" ? errorInfo.message : "Something went wrong. Please try again 2.", }); } }; // if the user doesn't have a password set, redirect to the profile page useEffect(() => { if (!currentUser) return; if (currentUser.is_password_autoset) router.push("/profile"); else setIsPageLoading(false); }, [currentUser, router]); const isButtonDisabled = getPasswordStrength(password) < 3 || oldPassword.trim() === "" || password.trim() === "" || confirmPassword.trim() === "" || password !== confirmPassword || password === oldPassword; const passwordSupport = password.length > 0 && (getPasswordStrength(password) < 3 || isPasswordInputFocused) && ( ); if (isPageLoading) return (
); const renderPasswordMatchError = !isRetryPasswordInputFocused || confirmPassword.length >= password.length; return ( <>
toggleSidebar()} />

Change password

Current password

( )} /> {showPassword?.oldPassword ? ( handleShowPassword("oldPassword")} /> ) : ( handleShowPassword("oldPassword")} /> )}
{errors.old_password && {errors.old_password.message}}

New password

( setIsPasswordInputFocused(true)} onBlur={() => setIsPasswordInputFocused(false)} /> )} /> {showPassword?.password ? ( handleShowPassword("password")} /> ) : ( handleShowPassword("password")} /> )}
{passwordSupport} {isNewPasswordSameAsOldPassword && !isPasswordInputFocused && ( New password must be different from old password )}

Confirm password

( setIsRetryPasswordInputFocused(true)} onBlur={() => setIsRetryPasswordInputFocused(false)} /> )} /> {showPassword?.confirmPassword ? ( handleShowPassword("confirmPassword")} /> ) : ( handleShowPassword("confirmPassword")} /> )}
{!!confirmPassword && password !== confirmPassword && renderPasswordMatchError && ( Passwords don{"'"}t match )}
); }); ChangePasswordPage.getLayout = function getLayout(page: ReactElement) { return {page}; }; export default ChangePasswordPage;