2024-05-08 17:31:20 +00:00
|
|
|
import React, { useEffect, useMemo, useState } from "react";
|
|
|
|
import { observer } from "mobx-react";
|
|
|
|
import Link from "next/link";
|
|
|
|
// icons
|
|
|
|
import { Eye, EyeOff, XCircle } from "lucide-react";
|
|
|
|
// ui
|
|
|
|
import { Button, Input, Spinner } from "@plane/ui";
|
|
|
|
// components
|
|
|
|
import { ForgotPasswordPopover, PasswordStrengthMeter } from "@/components/account";
|
|
|
|
// constants
|
|
|
|
import { FORGOT_PASSWORD } from "@/constants/event-tracker";
|
|
|
|
// helpers
|
|
|
|
import { EAuthModes, EAuthSteps } from "@/helpers/authentication.helper";
|
|
|
|
import { API_BASE_URL } from "@/helpers/common.helper";
|
|
|
|
import { getPasswordStrength } from "@/helpers/password.helper";
|
|
|
|
// hooks
|
2024-05-10 12:00:38 +00:00
|
|
|
import { useEventTracker } from "@/hooks/store";
|
2024-05-08 17:31:20 +00:00
|
|
|
// services
|
|
|
|
import { AuthService } from "@/services/auth.service";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
email: string;
|
2024-05-10 12:00:38 +00:00
|
|
|
isPasswordAutoset: boolean;
|
|
|
|
isSMTPConfigured: boolean;
|
2024-05-08 17:31:20 +00:00
|
|
|
mode: EAuthModes;
|
|
|
|
handleEmailClear: () => void;
|
2024-05-10 12:00:38 +00:00
|
|
|
handleAuthStep: (step: EAuthSteps) => void;
|
2024-05-08 17:31:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type TPasswordFormValues = {
|
|
|
|
email: string;
|
|
|
|
password: string;
|
|
|
|
confirm_password?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: TPasswordFormValues = {
|
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
const authService = new AuthService();
|
|
|
|
|
|
|
|
export const AuthPasswordForm: React.FC<Props> = observer((props: Props) => {
|
2024-05-10 12:00:38 +00:00
|
|
|
const { email, isSMTPConfigured, handleAuthStep, handleEmailClear, mode } = props;
|
2024-05-08 17:31:20 +00:00
|
|
|
// hooks
|
|
|
|
const { captureEvent } = useEventTracker();
|
2024-05-09 12:16:31 +00:00
|
|
|
// states
|
|
|
|
const [csrfToken, setCsrfToken] = useState<string | undefined>(undefined);
|
|
|
|
const [passwordFormData, setPasswordFormData] = useState<TPasswordFormValues>({ ...defaultValues, email });
|
|
|
|
const [showPassword, setShowPassword] = useState({
|
|
|
|
password: false,
|
|
|
|
retypePassword: false,
|
|
|
|
});
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
const [isPasswordInputFocused, setIsPasswordInputFocused] = useState(false);
|
2024-05-15 13:41:31 +00:00
|
|
|
const [isRetryPasswordInputFocused, setIsRetryPasswordInputFocused] = useState(false);
|
2024-05-09 12:16:31 +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 TPasswordFormValues, value: string) =>
|
|
|
|
setPasswordFormData((prev) => ({ ...prev, [key]: value }));
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (csrfToken === undefined)
|
|
|
|
authService.requestCSRFToken().then((data) => data?.csrf_token && setCsrfToken(data.csrf_token));
|
|
|
|
}, [csrfToken]);
|
|
|
|
|
|
|
|
const redirectToUniqueCodeSignIn = async () => {
|
2024-05-10 12:00:38 +00:00
|
|
|
handleAuthStep(EAuthSteps.UNIQUE_CODE);
|
2024-05-08 17:31:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const passwordSupport =
|
|
|
|
mode === EAuthModes.SIGN_IN ? (
|
2024-05-10 12:00:38 +00:00
|
|
|
<div className="w-full">
|
|
|
|
{isSMTPConfigured ? (
|
2024-05-08 17:31:20 +00:00
|
|
|
<Link
|
|
|
|
onClick={() => captureEvent(FORGOT_PASSWORD)}
|
|
|
|
href={`/accounts/forgot-password?email=${email}`}
|
|
|
|
className="text-xs font-medium text-custom-primary-100"
|
|
|
|
>
|
|
|
|
Forgot your password?
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
<ForgotPasswordPopover />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
) : (
|
2024-05-10 12:00:38 +00:00
|
|
|
passwordFormData.password.length > 0 &&
|
|
|
|
(getPasswordStrength(passwordFormData.password) < 3 || isPasswordInputFocused) && (
|
|
|
|
<PasswordStrengthMeter password={passwordFormData.password} />
|
|
|
|
)
|
2024-05-08 17:31:20 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const isButtonDisabled = useMemo(
|
|
|
|
() =>
|
|
|
|
!isSubmitting &&
|
|
|
|
!!passwordFormData.password &&
|
|
|
|
(mode === EAuthModes.SIGN_UP
|
|
|
|
? getPasswordStrength(passwordFormData.password) >= 3 &&
|
|
|
|
passwordFormData.password === passwordFormData.confirm_password
|
|
|
|
: true)
|
|
|
|
? false
|
|
|
|
: true,
|
|
|
|
[isSubmitting, mode, passwordFormData.confirm_password, passwordFormData.password]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form
|
|
|
|
className="mt-5 space-y-4"
|
|
|
|
method="POST"
|
|
|
|
action={`${API_BASE_URL}/auth/${mode === EAuthModes.SIGN_IN ? "sign-in" : "sign-up"}/`}
|
|
|
|
onSubmit={() => setIsSubmitting(true)}
|
|
|
|
onError={() => setIsSubmitting(false)}
|
|
|
|
>
|
|
|
|
<input type="hidden" name="csrfmiddlewaretoken" value={csrfToken} />
|
2024-05-10 12:00:38 +00:00
|
|
|
<input type="hidden" value={passwordFormData.email} name="email" />
|
2024-05-08 17:31:20 +00:00
|
|
|
<div className="space-y-1">
|
2024-05-10 12:00:38 +00:00
|
|
|
<label className="text-sm font-medium text-onboarding-text-300" htmlFor="email">
|
2024-05-08 17:31:20 +00:00
|
|
|
Email
|
|
|
|
</label>
|
2024-05-10 12:00:38 +00:00
|
|
|
<div
|
|
|
|
className={`relative flex items-center rounded-md bg-onboarding-background-200 border border-onboarding-border-100`}
|
|
|
|
>
|
2024-05-08 17:31:20 +00:00
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
type="email"
|
|
|
|
value={passwordFormData.email}
|
|
|
|
onChange={(e) => handleFormChange("email", e.target.value)}
|
|
|
|
placeholder="name@company.com"
|
2024-05-10 12:00:38 +00:00
|
|
|
className={`disable-autofill-style h-[46px] w-full placeholder:text-onboarding-text-400 border-0`}
|
2024-05-09 12:16:31 +00:00
|
|
|
disabled
|
2024-05-08 17:31:20 +00:00
|
|
|
/>
|
|
|
|
{passwordFormData.email.length > 0 && (
|
2024-05-10 12:00:38 +00:00
|
|
|
<div className="flex-shrink-0 h-5 w-5 mr-2 bg-onboarding-background-200 hover:cursor-pointer">
|
|
|
|
<XCircle className="h-5 w-5 stroke-custom-text-400" onClick={handleEmailClear} />
|
|
|
|
</div>
|
2024-05-08 17:31:20 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-05-10 12:00:38 +00:00
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
<div className="space-y-1">
|
|
|
|
<label className="text-sm text-onboarding-text-300 font-medium" htmlFor="password">
|
|
|
|
{mode === EAuthModes.SIGN_IN ? "Password" : "Set a password"}
|
|
|
|
</label>
|
|
|
|
<div className="relative flex items-center rounded-md bg-onboarding-background-200">
|
|
|
|
<Input
|
2024-05-09 12:16:31 +00:00
|
|
|
type={showPassword?.password ? "text" : "password"}
|
2024-05-08 17:31:20 +00:00
|
|
|
name="password"
|
|
|
|
value={passwordFormData.password}
|
|
|
|
onChange={(e) => handleFormChange("password", e.target.value)}
|
|
|
|
placeholder="Enter password"
|
2024-05-10 12:00:38 +00:00
|
|
|
className="disable-autofill-style h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 placeholder:text-onboarding-text-400"
|
2024-05-08 17:31:20 +00:00
|
|
|
onFocus={() => setIsPasswordInputFocused(true)}
|
|
|
|
onBlur={() => setIsPasswordInputFocused(false)}
|
|
|
|
autoFocus
|
|
|
|
/>
|
2024-05-09 12:16:31 +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-09 12:16:31 +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-09 12:16:31 +00:00
|
|
|
onClick={() => handleShowPassword("password")}
|
2024-05-08 17:31:20 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
{passwordSupport}
|
|
|
|
</div>
|
2024-05-10 12:00:38 +00:00
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
{mode === EAuthModes.SIGN_UP && (
|
|
|
|
<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-09 12:16:31 +00:00
|
|
|
type={showPassword?.retypePassword ? "text" : "password"}
|
2024-05-08 17:31:20 +00:00
|
|
|
name="confirm_password"
|
|
|
|
value={passwordFormData.confirm_password}
|
|
|
|
onChange={(e) => handleFormChange("confirm_password", e.target.value)}
|
|
|
|
placeholder="Confirm password"
|
2024-05-10 12:00:38 +00:00
|
|
|
className="disable-autofill-style 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-09 12:16:31 +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-09 12:16:31 +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-09 12:16:31 +00:00
|
|
|
onClick={() => handleShowPassword("retypePassword")}
|
2024-05-08 17:31:20 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
2024-05-15 13:41:31 +00:00
|
|
|
{!!passwordFormData.confirm_password &&
|
|
|
|
passwordFormData.password !== passwordFormData.confirm_password &&
|
|
|
|
!isRetryPasswordInputFocused && <span className="text-sm text-red-500">Passwords don{"'"}t match</span>}
|
2024-05-08 17:31:20 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2024-05-10 12:00:38 +00:00
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
<div className="space-y-2.5">
|
|
|
|
{mode === EAuthModes.SIGN_IN ? (
|
|
|
|
<>
|
|
|
|
<Button type="submit" variant="primary" className="w-full" size="lg" disabled={isButtonDisabled}>
|
|
|
|
{isSubmitting ? (
|
|
|
|
<Spinner height="20px" width="20px" />
|
2024-05-10 12:00:38 +00:00
|
|
|
) : isSMTPConfigured ? (
|
2024-05-08 17:31:20 +00:00
|
|
|
"Continue"
|
|
|
|
) : (
|
|
|
|
"Go to workspace"
|
|
|
|
)}
|
|
|
|
</Button>
|
2024-05-10 12:00:38 +00:00
|
|
|
{isSMTPConfigured && (
|
2024-05-08 17:31:20 +00:00
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
onClick={redirectToUniqueCodeSignIn}
|
|
|
|
variant="outline-primary"
|
|
|
|
className="w-full"
|
|
|
|
size="lg"
|
|
|
|
>
|
|
|
|
Sign in with unique code
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<Button type="submit" variant="primary" className="w-full" size="lg" disabled={isButtonDisabled}>
|
|
|
|
{isSubmitting ? <Spinner height="20px" width="20px" /> : "Create account"}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
});
|