"use client"; import { useEffect, useState } from "react"; import { observer } from "mobx-react"; import { useRouter } from "next/navigation"; 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 { useUser } from "@/hooks/store"; // 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: "", }; const userService = new UserService(); const authService = new AuthService(); const defaultShowPassword = { oldPassword: false, password: false, confirmPassword: false, }; const SecurityPage = 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(); 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 ( <>

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 )}
); }); export default SecurityPage;