2024-05-14 15:23:51 +00:00
|
|
|
import { ReactElement, useEffect, useMemo, useState } from "react";
|
2024-05-08 17:31:20 +00:00
|
|
|
import { observer } from "mobx-react";
|
2024-03-06 13:09:14 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-11-23 15:08:50 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2024-05-14 15:23:51 +00:00
|
|
|
import { Eye, EyeOff } from "lucide-react";
|
2024-05-08 17:31:20 +00:00
|
|
|
// ui
|
2024-03-06 13:09:14 +00:00
|
|
|
import { Button, Input, Spinner, TOAST_TYPE, setPromiseToast, setToast } from "@plane/ui";
|
2024-02-20 08:06:38 +00:00
|
|
|
// components
|
2024-05-14 15:23:51 +00:00
|
|
|
import { PasswordStrengthMeter } from "@/components/account";
|
2024-05-08 17:31:20 +00:00
|
|
|
import { PageHead } from "@/components/core";
|
|
|
|
import { SidebarHamburgerToggle } from "@/components/core/sidebar";
|
2024-05-14 15:23:51 +00:00
|
|
|
import { getPasswordStrength } from "@/helpers/password.helper";
|
2024-05-08 17:31:20 +00:00
|
|
|
// hooks
|
|
|
|
import { useAppTheme, useUser } from "@/hooks/store";
|
2023-11-23 15:08:50 +00:00
|
|
|
// layout
|
2024-03-19 14:38:35 +00:00
|
|
|
import { ProfileSettingsLayout } from "@/layouts/settings-layout";
|
2023-11-23 15:08:50 +00:00
|
|
|
// types
|
2024-03-19 14:38:35 +00:00
|
|
|
import { NextPageWithLayout } from "@/lib/types";
|
2024-05-08 17:31:20 +00:00
|
|
|
// services
|
2024-05-14 15:23:51 +00:00
|
|
|
import { AuthService } from "@/services/auth.service";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { UserService } from "@/services/user.service";
|
2023-11-23 15:08:50 +00:00
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
export interface FormValues {
|
2023-11-23 15:08:50 +00:00
|
|
|
old_password: string;
|
|
|
|
new_password: string;
|
|
|
|
confirm_password: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultValues: FormValues = {
|
|
|
|
old_password: "",
|
|
|
|
new_password: "",
|
|
|
|
confirm_password: "",
|
|
|
|
};
|
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
export const userService = new UserService();
|
2024-05-14 15:23:51 +00:00
|
|
|
export const authService = new AuthService();
|
2023-11-23 15:08:50 +00:00
|
|
|
|
|
|
|
const ChangePasswordPage: NextPageWithLayout = observer(() => {
|
2024-05-14 15:23:51 +00:00
|
|
|
const [csrfToken, setCsrfToken] = useState<string | undefined>(undefined);
|
2023-11-23 15:08:50 +00:00
|
|
|
const [isPageLoading, setIsPageLoading] = useState(true);
|
2024-05-14 15:23:51 +00:00
|
|
|
const [showPassword, setShowPassword] = useState({
|
|
|
|
oldPassword: false,
|
|
|
|
password: false,
|
|
|
|
retypePassword: false,
|
|
|
|
});
|
|
|
|
const [isPasswordInputFocused, setIsPasswordInputFocused] = useState(false);
|
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
// router
|
2023-11-23 15:08:50 +00:00
|
|
|
const router = useRouter();
|
2024-05-08 17:31:20 +00:00
|
|
|
// store hooks
|
|
|
|
const { toggleSidebar } = useAppTheme();
|
|
|
|
const { data: currentUser } = useUser();
|
2023-11-23 15:08:50 +00:00
|
|
|
|
|
|
|
// use form
|
|
|
|
const {
|
|
|
|
control,
|
|
|
|
handleSubmit,
|
2024-05-14 15:23:51 +00:00
|
|
|
watch,
|
2023-11-23 15:08:50 +00:00
|
|
|
formState: { errors, isSubmitting },
|
2024-05-14 15:23:51 +00:00
|
|
|
reset,
|
2023-11-23 15:08:50 +00:00
|
|
|
} = useForm<FormValues>({ defaultValues });
|
|
|
|
|
2024-05-14 15:23:51 +00:00
|
|
|
const oldPassword = watch("old_password");
|
|
|
|
const password = watch("new_password");
|
|
|
|
const retypePassword = watch("confirm_password");
|
|
|
|
|
|
|
|
const handleShowPassword = (key: keyof typeof showPassword) =>
|
|
|
|
setShowPassword((prev) => ({ ...prev, [key]: !prev[key] }));
|
|
|
|
|
2023-11-23 15:08:50 +00:00
|
|
|
const handleChangePassword = async (formData: FormValues) => {
|
2024-05-14 15:23:51 +00:00
|
|
|
try {
|
|
|
|
if (!csrfToken) throw new Error("csrf token not found");
|
|
|
|
const changePasswordPromise = userService
|
|
|
|
.changePassword(csrfToken, {
|
|
|
|
old_password: formData.old_password,
|
|
|
|
new_password: formData.new_password,
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
reset(defaultValues);
|
|
|
|
});
|
|
|
|
setPromiseToast(changePasswordPromise, {
|
|
|
|
loading: "Changing password...",
|
|
|
|
success: {
|
|
|
|
title: "Success!",
|
|
|
|
message: () => "Password changed successfully.",
|
|
|
|
},
|
|
|
|
error: {
|
|
|
|
title: "Error!",
|
|
|
|
message: () => "Something went wrong. Please try again 1.",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (err: any) {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2023-11-23 15:08:50 +00:00
|
|
|
title: "Error!",
|
2024-05-14 15:23:51 +00:00
|
|
|
message: err?.error ?? "Something went wrong. Please try again 2.",
|
2023-11-23 15:08:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-14 15:23:51 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (csrfToken === undefined)
|
|
|
|
authService.requestCSRFToken().then((data) => data?.csrf_token && setCsrfToken(data.csrf_token));
|
|
|
|
}, [csrfToken]);
|
|
|
|
|
2023-11-23 15:08:50 +00:00
|
|
|
useEffect(() => {
|
2023-12-11 12:00:17 +00:00
|
|
|
if (!currentUser) return;
|
2023-11-23 15:08:50 +00:00
|
|
|
|
2023-12-11 12:00:17 +00:00
|
|
|
if (currentUser.is_password_autoset) router.push("/profile");
|
2023-11-23 15:08:50 +00:00
|
|
|
else setIsPageLoading(false);
|
2023-12-11 12:00:17 +00:00
|
|
|
}, [currentUser, router]);
|
2023-11-23 15:08:50 +00:00
|
|
|
|
2024-05-14 15:23:51 +00:00
|
|
|
const isButtonDisabled = useMemo(
|
|
|
|
() =>
|
|
|
|
!isSubmitting &&
|
|
|
|
!!oldPassword &&
|
|
|
|
!!password &&
|
|
|
|
!!retypePassword &&
|
|
|
|
getPasswordStrength(password) >= 3 &&
|
|
|
|
password === retypePassword &&
|
|
|
|
password !== oldPassword
|
|
|
|
? false
|
|
|
|
: true,
|
|
|
|
|
|
|
|
[isSubmitting, oldPassword, password, retypePassword]
|
|
|
|
);
|
|
|
|
|
|
|
|
const passwordSupport = password.length > 0 && (getPasswordStrength(password) < 3 || isPasswordInputFocused) && (
|
|
|
|
<PasswordStrengthMeter password={password} />
|
|
|
|
);
|
|
|
|
|
2023-11-23 15:08:50 +00:00
|
|
|
if (isPageLoading)
|
|
|
|
return (
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="grid h-screen w-full place-items-center">
|
2023-11-23 15:08:50 +00:00
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2024-02-20 08:06:38 +00:00
|
|
|
<>
|
|
|
|
<PageHead title="Profile - Change Password" />
|
2024-03-06 13:09:14 +00:00
|
|
|
<div className="flex h-full flex-col">
|
|
|
|
<div className="block flex-shrink-0 border-b border-custom-border-200 p-4 md:hidden">
|
2024-05-08 17:31:20 +00:00
|
|
|
<SidebarHamburgerToggle onClick={() => toggleSidebar()} />
|
2024-02-20 08:06:38 +00:00
|
|
|
</div>
|
|
|
|
<form
|
|
|
|
onSubmit={handleSubmit(handleChangePassword)}
|
2024-04-29 07:29:49 +00:00
|
|
|
className="mx-auto md:mt-16 mt-10 flex h-full w-full flex-col gap-8 px-4 md:px-8 pb-8 lg:w-3/5"
|
2024-02-20 08:06:38 +00:00
|
|
|
>
|
2024-05-14 15:23:51 +00:00
|
|
|
<input type="hidden" name="csrfmiddlewaretoken" value={csrfToken} />
|
|
|
|
|
2024-02-20 08:06:38 +00:00
|
|
|
<h3 className="text-xl font-medium">Change password</h3>
|
|
|
|
<div className="grid-col grid w-full grid-cols-1 items-center justify-between gap-10 xl:grid-cols-2 2xl:grid-cols-3">
|
|
|
|
<div className="flex flex-col gap-1 ">
|
|
|
|
<h4 className="text-sm">Current password</h4>
|
2024-05-14 15:23:51 +00:00
|
|
|
<div className="relative flex items-center rounded-md">
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="old_password"
|
|
|
|
rules={{
|
|
|
|
required: "This field is required",
|
|
|
|
}}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<Input
|
|
|
|
id="old_password"
|
|
|
|
type={showPassword?.oldPassword ? "text" : "password"}
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
placeholder="Old password"
|
|
|
|
className="w-full rounded-md font-medium"
|
|
|
|
hasError={Boolean(errors.old_password)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
{showPassword?.oldPassword ? (
|
|
|
|
<EyeOff
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
|
|
onClick={() => handleShowPassword("oldPassword")}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Eye
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
|
|
onClick={() => handleShowPassword("oldPassword")}
|
2024-02-20 08:06:38 +00:00
|
|
|
/>
|
|
|
|
)}
|
2024-05-14 15:23:51 +00:00
|
|
|
</div>
|
|
|
|
|
2024-02-20 08:06:38 +00:00
|
|
|
{errors.old_password && <span className="text-xs text-red-500">{errors.old_password.message}</span>}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-col gap-1 ">
|
|
|
|
<h4 className="text-sm">New password</h4>
|
2024-05-14 15:23:51 +00:00
|
|
|
<div className="relative flex items-center rounded-md">
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="new_password"
|
|
|
|
rules={{
|
|
|
|
required: "This field is required",
|
|
|
|
}}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<Input
|
|
|
|
id="new_password"
|
|
|
|
type={showPassword?.password ? "text" : "password"}
|
|
|
|
value={value}
|
|
|
|
placeholder="New password"
|
|
|
|
onChange={onChange}
|
|
|
|
className="w-full"
|
|
|
|
hasError={Boolean(errors.new_password)}
|
|
|
|
onFocus={() => setIsPasswordInputFocused(true)}
|
|
|
|
onBlur={() => setIsPasswordInputFocused(false)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
{showPassword?.password ? (
|
|
|
|
<EyeOff
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
|
|
onClick={() => handleShowPassword("password")}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Eye
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
|
|
onClick={() => handleShowPassword("password")}
|
2024-02-20 08:06:38 +00:00
|
|
|
/>
|
|
|
|
)}
|
2024-05-14 15:23:51 +00:00
|
|
|
</div>
|
|
|
|
{passwordSupport}
|
2024-02-20 08:06:38 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-col gap-1 ">
|
|
|
|
<h4 className="text-sm">Confirm password</h4>
|
2024-05-14 15:23:51 +00:00
|
|
|
<div className="relative flex items-center rounded-md">
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="confirm_password"
|
|
|
|
rules={{
|
|
|
|
required: "This field is required",
|
|
|
|
}}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<Input
|
|
|
|
id="confirm_password"
|
|
|
|
type={showPassword?.retypePassword ? "text" : "password"}
|
|
|
|
placeholder="Confirm password"
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
className="w-full"
|
|
|
|
hasError={Boolean(errors.confirm_password)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
{showPassword?.retypePassword ? (
|
|
|
|
<EyeOff
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
|
|
onClick={() => handleShowPassword("retypePassword")}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Eye
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
|
|
onClick={() => handleShowPassword("retypePassword")}
|
2024-02-20 08:06:38 +00:00
|
|
|
/>
|
|
|
|
)}
|
2024-05-14 15:23:51 +00:00
|
|
|
</div>
|
|
|
|
{!!retypePassword && password !== retypePassword && (
|
|
|
|
<span className="text-sm text-red-500">Passwords don{"'"}t match</span>
|
2024-02-14 09:25:33 +00:00
|
|
|
)}
|
2024-02-20 08:06:38 +00:00
|
|
|
</div>
|
2024-02-14 09:25:33 +00:00
|
|
|
</div>
|
|
|
|
|
2024-02-20 08:06:38 +00:00
|
|
|
<div className="flex items-center justify-between py-2">
|
2024-05-14 15:23:51 +00:00
|
|
|
<Button variant="primary" type="submit" loading={isSubmitting} disabled={isButtonDisabled}>
|
2024-02-20 08:06:38 +00:00
|
|
|
{isSubmitting ? "Changing password..." : "Change password"}
|
|
|
|
</Button>
|
2024-02-14 09:25:33 +00:00
|
|
|
</div>
|
2024-02-20 08:06:38 +00:00
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</>
|
2023-11-23 15:08:50 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
ChangePasswordPage.getLayout = function getLayout(page: ReactElement) {
|
2023-11-29 08:18:07 +00:00
|
|
|
return <ProfileSettingsLayout>{page}</ProfileSettingsLayout>;
|
2023-11-23 15:08:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ChangePasswordPage;
|