"use client"; import { useState } from "react"; import { observer } from "mobx-react"; import { Controller, useForm } from "react-hook-form"; import { Eye, EyeOff } from "lucide-react"; import { useTranslation } from "@plane/i18n"; // ui import { Button, Input, TOAST_TYPE, setToast } from "@plane/ui"; // components import { PasswordStrengthMeter } from "@/components/account"; import { PageHead } from "@/components/core"; import { ProfileSettingContentHeader, ProfileSettingContentWrapper } from "@/components/profile"; // helpers import { authErrorHandler } from "@/helpers/authentication.helper"; import { E_PASSWORD_STRENGTH, getPasswordStrength } from "@/helpers/password.helper"; // 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 [showPassword, setShowPassword] = useState(defaultShowPassword); const [isPasswordInputFocused, setIsPasswordInputFocused] = useState(false); const [isRetryPasswordInputFocused, setIsRetryPasswordInputFocused] = useState(false); // 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"); // i18n const { t } = useTranslation(); 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: t("auth.common.password.toast.change_password.success.title"), message: t("auth.common.password.toast.change_password.success.message"), }); } catch (err: any) { const errorInfo = authErrorHandler(err.error_code?.toString()); setToast({ type: TOAST_TYPE.ERROR, title: errorInfo?.title ?? t("auth.common.password.toast.error.title"), message: typeof errorInfo?.message === "string" ? errorInfo.message : t("auth.common.password.toast.error.message"), }); } }; const isButtonDisabled = getPasswordStrength(password) != E_PASSWORD_STRENGTH.STRENGTH_VALID || oldPassword.trim() === "" || password.trim() === "" || confirmPassword.trim() === "" || password !== confirmPassword || password === oldPassword; const passwordSupport = password.length > 0 && getPasswordStrength(password) != E_PASSWORD_STRENGTH.STRENGTH_VALID && ( ); const renderPasswordMatchError = !isRetryPasswordInputFocused || confirmPassword.length >= password.length; return ( <>

{t("auth.common.password.current_password.label")}

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

{t("auth.common.password.new_password.label")}

( setIsPasswordInputFocused(true)} onBlur={() => setIsPasswordInputFocused(false)} /> )} /> {showPassword?.password ? ( handleShowPassword("password")} /> ) : ( handleShowPassword("password")} /> )}
{passwordSupport} {isNewPasswordSameAsOldPassword && !isPasswordInputFocused && ( {t("new_password_must_be_different_from_old_password")} )}

{t("auth.common.password.confirm_password.label")}

( setIsRetryPasswordInputFocused(true)} onBlur={() => setIsRetryPasswordInputFocused(false)} /> )} /> {showPassword?.confirmPassword ? ( handleShowPassword("confirmPassword")} /> ) : ( handleShowPassword("confirmPassword")} /> )}
{!!confirmPassword && password !== confirmPassword && renderPasswordMatchError && ( {t("auth.common.password.errors.match")} )}
); }); export default SecurityPage;