2024-05-08 17:31:20 +00:00
|
|
|
import { ReactElement, useEffect, useMemo, useState } from "react";
|
2023-11-29 13:37:33 +00:00
|
|
|
import Image from "next/image";
|
|
|
|
import { useRouter } from "next/router";
|
2024-05-08 17:31:20 +00:00
|
|
|
// icons
|
|
|
|
import { useTheme } from "next-themes";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { Eye, EyeOff } from "lucide-react";
|
2024-05-08 17:31:20 +00:00
|
|
|
// ui
|
|
|
|
import { Button, Input } from "@plane/ui";
|
|
|
|
// components
|
2024-05-20 09:20:11 +00:00
|
|
|
import { AuthBanner, PasswordStrengthMeter } from "@/components/account";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { PageHead } from "@/components/core";
|
2024-05-08 17:31:20 +00:00
|
|
|
// helpers
|
2024-05-20 09:20:11 +00:00
|
|
|
import {
|
|
|
|
EAuthenticationErrorCodes,
|
|
|
|
EErrorAlertType,
|
|
|
|
EPageTypes,
|
|
|
|
TAuthErrorInfo,
|
|
|
|
authErrorHandler,
|
|
|
|
} from "@/helpers/authentication.helper";
|
2024-05-08 17:31:20 +00:00
|
|
|
import { API_BASE_URL } from "@/helpers/common.helper";
|
|
|
|
import { getPasswordStrength } from "@/helpers/password.helper";
|
|
|
|
// layouts
|
2024-03-19 14:38:35 +00:00
|
|
|
import DefaultLayout from "@/layouts/default-layout";
|
2024-05-08 17:31:20 +00:00
|
|
|
// lib
|
2024-03-19 14:38:35 +00:00
|
|
|
import { NextPageWithLayout } from "@/lib/types";
|
2024-05-08 17:31:20 +00:00
|
|
|
// wrappers
|
|
|
|
import { AuthenticationWrapper } from "@/lib/wrappers";
|
|
|
|
// services
|
2024-03-19 14:38:35 +00:00
|
|
|
import { AuthService } from "@/services/auth.service";
|
2023-11-29 13:37:33 +00:00
|
|
|
// images
|
2024-05-08 17:31:20 +00:00
|
|
|
import PlaneBackgroundPatternDark from "public/auth/background-pattern-dark.svg";
|
|
|
|
import PlaneBackgroundPattern from "public/auth/background-pattern.svg";
|
2023-11-29 13:37:33 +00:00
|
|
|
import BluePlaneLogoWithoutText from "public/plane-logos/blue-without-text.png";
|
|
|
|
|
|
|
|
type TResetPasswordFormValues = {
|
|
|
|
email: string;
|
|
|
|
password: string;
|
2024-05-08 17:31:20 +00:00
|
|
|
confirm_password?: string;
|
2023-11-29 13:37:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: TResetPasswordFormValues = {
|
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
// services
|
|
|
|
const authService = new AuthService();
|
|
|
|
|
2024-01-19 15:25:03 +00:00
|
|
|
const ResetPasswordPage: NextPageWithLayout = () => {
|
2023-11-29 13:37:33 +00:00
|
|
|
// router
|
|
|
|
const router = useRouter();
|
2024-05-20 09:20:11 +00:00
|
|
|
const { uidb64, token, email, error_code } = router.query;
|
2024-01-30 14:41:16 +00:00
|
|
|
// states
|
2024-05-17 10:21:34 +00:00
|
|
|
const [showPassword, setShowPassword] = useState({
|
|
|
|
password: false,
|
|
|
|
retypePassword: false,
|
|
|
|
});
|
2024-05-08 17:31:20 +00:00
|
|
|
const [resetFormData, setResetFormData] = useState<TResetPasswordFormValues>({
|
|
|
|
...defaultValues,
|
|
|
|
email: email ? email.toString() : "",
|
2023-11-29 13:37:33 +00:00
|
|
|
});
|
2024-05-08 17:31:20 +00:00
|
|
|
const [csrfToken, setCsrfToken] = useState<string | undefined>(undefined);
|
|
|
|
const [isPasswordInputFocused, setIsPasswordInputFocused] = useState(false);
|
2024-05-15 13:41:31 +00:00
|
|
|
const [isRetryPasswordInputFocused, setIsRetryPasswordInputFocused] = useState(false);
|
2024-05-20 09:20:11 +00:00
|
|
|
const [errorInfo, setErrorInfo] = useState<TAuthErrorInfo | undefined>(undefined);
|
2024-05-15 13:41:31 +00:00
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
// hooks
|
|
|
|
const { resolvedTheme } = useTheme();
|
2023-11-29 13:37:33 +00:00
|
|
|
|
2024-05-17 10:21:34 +00:00
|
|
|
const handleShowPassword = (key: keyof typeof showPassword) =>
|
|
|
|
setShowPassword((prev) => ({ ...prev, [key]: !prev[key] }));
|
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
const handleFormChange = (key: keyof TResetPasswordFormValues, value: string) =>
|
|
|
|
setResetFormData((prev) => ({ ...prev, [key]: value }));
|
2023-11-29 13:37:33 +00:00
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (csrfToken === undefined)
|
|
|
|
authService.requestCSRFToken().then((data) => data?.csrf_token && setCsrfToken(data.csrf_token));
|
|
|
|
}, [csrfToken]);
|
2023-11-29 13:37:33 +00:00
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
const isButtonDisabled = useMemo(
|
|
|
|
() =>
|
|
|
|
!!resetFormData.password &&
|
|
|
|
getPasswordStrength(resetFormData.password) >= 3 &&
|
|
|
|
resetFormData.password === resetFormData.confirm_password
|
|
|
|
? false
|
|
|
|
: true,
|
|
|
|
[resetFormData]
|
|
|
|
);
|
2023-11-29 13:37:33 +00:00
|
|
|
|
2024-05-20 09:20:11 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (error_code) {
|
|
|
|
const errorhandler = authErrorHandler(error_code?.toString() as EAuthenticationErrorCodes);
|
|
|
|
if (errorhandler) {
|
|
|
|
setErrorInfo(errorhandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [error_code]);
|
|
|
|
|
2024-05-20 13:33:23 +00:00
|
|
|
const password = resetFormData?.password ?? "";
|
|
|
|
const confirmPassword = resetFormData?.confirm_password ?? "";
|
|
|
|
const renderPasswordMatchError = !isRetryPasswordInputFocused || confirmPassword.length >= password.length;
|
|
|
|
|
2023-11-29 13:37:33 +00:00
|
|
|
return (
|
2024-05-08 17:31:20 +00:00
|
|
|
<div className="relative w-screen h-screen overflow-hidden">
|
2024-02-20 08:06:38 +00:00
|
|
|
<PageHead title="Reset Password" />
|
2024-05-08 17:31:20 +00:00
|
|
|
<div className="absolute inset-0 z-0">
|
|
|
|
<Image
|
|
|
|
src={resolvedTheme === "dark" ? PlaneBackgroundPatternDark : PlaneBackgroundPattern}
|
|
|
|
className="w-full h-full object-cover"
|
|
|
|
alt="Plane background pattern"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="relative z-10 w-screen h-screen overflow-hidden overflow-y-auto flex flex-col">
|
|
|
|
<div className="container mx-auto px-10 lg:px-0 flex-shrink-0 relative flex items-center justify-between pb-4 transition-all">
|
2024-02-20 08:06:38 +00:00
|
|
|
<div className="flex items-center gap-x-2 py-10">
|
2024-05-08 17:31:20 +00:00
|
|
|
<Image src={BluePlaneLogoWithoutText} height={30} width={30} alt="Plane Logo" />
|
2024-02-20 08:06:38 +00:00
|
|
|
<span className="text-2xl font-semibold sm:text-3xl">Plane</span>
|
|
|
|
</div>
|
2023-11-29 13:37:33 +00:00
|
|
|
</div>
|
2024-05-08 17:31:20 +00:00
|
|
|
<div className="flex-grow container mx-auto max-w-lg px-10 lg:max-w-md lg:px-5 py-10 lg:pt-28 transition-all">
|
|
|
|
<div className="relative flex flex-col space-y-6">
|
|
|
|
<div className="text-center space-y-1 py-4">
|
|
|
|
<h3 className="flex gap-4 justify-center text-3xl font-bold text-onboarding-text-100">
|
|
|
|
Set new password
|
|
|
|
</h3>
|
|
|
|
<p className="font-medium text-onboarding-text-400">Secure your account with a strong password</p>
|
|
|
|
</div>
|
2024-05-20 09:20:11 +00:00
|
|
|
{errorInfo && errorInfo?.type === EErrorAlertType.BANNER_ALERT && (
|
|
|
|
<AuthBanner bannerData={errorInfo} handleBannerData={(value) => setErrorInfo(value)} />
|
|
|
|
)}
|
2024-05-08 17:31:20 +00:00
|
|
|
<form
|
|
|
|
className="mt-5 space-y-4"
|
|
|
|
method="POST"
|
|
|
|
action={`${API_BASE_URL}/auth/reset-password/${uidb64?.toString()}/${token?.toString()}/`}
|
|
|
|
>
|
|
|
|
<input type="hidden" name="csrfmiddlewaretoken" value={csrfToken} />
|
|
|
|
<div className="space-y-1">
|
|
|
|
<label className="text-sm text-onboarding-text-300 font-medium" htmlFor="email">
|
|
|
|
Email
|
|
|
|
</label>
|
|
|
|
<div className="relative flex items-center rounded-md bg-onboarding-background-200">
|
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
type="email"
|
|
|
|
value={resetFormData.email}
|
|
|
|
//hasError={Boolean(errors.email)}
|
|
|
|
placeholder="name@company.com"
|
|
|
|
className="h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 text-onboarding-text-400 cursor-not-allowed"
|
|
|
|
disabled
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="space-y-1">
|
|
|
|
<label className="text-sm text-onboarding-text-300 font-medium" htmlFor="password">
|
|
|
|
Password
|
|
|
|
</label>
|
|
|
|
<div className="relative flex items-center rounded-md bg-onboarding-background-200">
|
|
|
|
<Input
|
2024-05-17 10:21:34 +00:00
|
|
|
type={showPassword.password ? "text" : "password"}
|
2024-05-08 17:31:20 +00:00
|
|
|
name="password"
|
|
|
|
value={resetFormData.password}
|
|
|
|
onChange={(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
|
|
|
|
/>
|
2024-05-17 10:21:34 +00:00
|
|
|
{showPassword.password ? (
|
2024-05-08 17:31:20 +00:00
|
|
|
<EyeOff
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
2024-05-17 10:21:34 +00:00
|
|
|
onClick={() => handleShowPassword("password")}
|
2024-05-08 17:31:20 +00:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Eye
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
2024-05-17 10:21:34 +00:00
|
|
|
onClick={() => handleShowPassword("password")}
|
2024-01-30 14:41:16 +00:00
|
|
|
/>
|
2024-02-20 08:06:38 +00:00
|
|
|
)}
|
2024-05-08 17:31:20 +00:00
|
|
|
</div>
|
|
|
|
{isPasswordInputFocused && <PasswordStrengthMeter password={resetFormData.password} />}
|
|
|
|
</div>
|
|
|
|
<div className="space-y-1">
|
|
|
|
<label className="text-sm text-onboarding-text-300 font-medium" htmlFor="confirm_password">
|
|
|
|
Confirm password
|
|
|
|
</label>
|
|
|
|
<div className="relative flex items-center rounded-md bg-onboarding-background-200">
|
|
|
|
<Input
|
2024-05-17 10:21:34 +00:00
|
|
|
type={showPassword.retypePassword ? "text" : "password"}
|
2024-05-08 17:31:20 +00:00
|
|
|
name="confirm_password"
|
|
|
|
value={resetFormData.confirm_password}
|
|
|
|
onChange={(e) => 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"
|
2024-05-15 13:41:31 +00:00
|
|
|
onFocus={() => setIsRetryPasswordInputFocused(true)}
|
|
|
|
onBlur={() => setIsRetryPasswordInputFocused(false)}
|
2024-05-08 17:31:20 +00:00
|
|
|
/>
|
2024-05-17 10:21:34 +00:00
|
|
|
{showPassword.retypePassword ? (
|
2024-05-08 17:31:20 +00:00
|
|
|
<EyeOff
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
2024-05-17 10:21:34 +00:00
|
|
|
onClick={() => handleShowPassword("retypePassword")}
|
2024-05-08 17:31:20 +00:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Eye
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
2024-05-17 10:21:34 +00:00
|
|
|
onClick={() => handleShowPassword("retypePassword")}
|
2024-05-08 17:31:20 +00:00
|
|
|
/>
|
2024-02-20 08:06:38 +00:00
|
|
|
)}
|
2024-05-08 17:31:20 +00:00
|
|
|
</div>
|
2024-05-15 13:41:31 +00:00
|
|
|
{!!resetFormData.confirm_password &&
|
|
|
|
resetFormData.password !== resetFormData.confirm_password &&
|
2024-05-20 13:33:23 +00:00
|
|
|
renderPasswordMatchError && <span className="text-sm text-red-500">Passwords don{"'"}t match</span>}
|
2024-05-08 17:31:20 +00:00
|
|
|
</div>
|
|
|
|
<Button type="submit" variant="primary" className="w-full" size="lg" disabled={isButtonDisabled}>
|
|
|
|
Set password
|
|
|
|
</Button>
|
|
|
|
</form>
|
2023-11-29 13:37:33 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-05-08 17:31:20 +00:00
|
|
|
</div>
|
2023-11-29 13:37:33 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-01-19 15:25:03 +00:00
|
|
|
ResetPasswordPage.getLayout = function getLayout(page: ReactElement) {
|
2024-05-08 17:31:20 +00:00
|
|
|
return (
|
|
|
|
<DefaultLayout>
|
|
|
|
<AuthenticationWrapper pageType={EPageTypes.NON_AUTHENTICATED}>{page}</AuthenticationWrapper>
|
|
|
|
</DefaultLayout>
|
|
|
|
);
|
2023-11-29 13:37:33 +00:00
|
|
|
};
|
|
|
|
|
2024-01-19 15:25:03 +00:00
|
|
|
export default ResetPasswordPage;
|