2024-04-29 06:42:33 +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
|
2024-05-01 20:49:18 +00:00
|
|
|
import { Button, Input, Spinner } from "@plane/ui";
|
2024-04-29 06:42:33 +00:00
|
|
|
// components
|
2024-04-30 14:00:42 +00:00
|
|
|
import { ForgotPasswordPopover, PasswordStrengthMeter } from "@/components/account";
|
2024-04-29 06:42:33 +00:00
|
|
|
// constants
|
|
|
|
import { FORGOT_PASSWORD } from "@/constants/event-tracker";
|
|
|
|
// helpers
|
2024-04-30 14:00:42 +00:00
|
|
|
import { EAuthModes, EAuthSteps } from "@/helpers/authentication.helper";
|
2024-04-29 06:42:33 +00:00
|
|
|
import { API_BASE_URL } from "@/helpers/common.helper";
|
|
|
|
import { getPasswordStrength } from "@/helpers/password.helper";
|
|
|
|
// hooks
|
|
|
|
import { useEventTracker, useInstance } from "@/hooks/store";
|
|
|
|
// services
|
|
|
|
import { AuthService } from "@/services/auth.service";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
email: string;
|
|
|
|
mode: EAuthModes;
|
|
|
|
handleStepChange: (step: EAuthSteps) => void;
|
|
|
|
handleEmailClear: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
const { email, handleStepChange, handleEmailClear, mode } = props;
|
|
|
|
// states
|
|
|
|
const [passwordFormData, setPasswordFormData] = useState<TPasswordFormValues>({ ...defaultValues, email });
|
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
|
|
const [csrfToken, setCsrfToken] = useState<string | undefined>(undefined);
|
|
|
|
const [isPasswordInputFocused, setIsPasswordInputFocused] = useState(false);
|
2024-05-01 20:49:18 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
2024-04-29 06:42:33 +00:00
|
|
|
// hooks
|
|
|
|
const { instance } = useInstance();
|
|
|
|
const { captureEvent } = useEventTracker();
|
|
|
|
// derived values
|
|
|
|
const isSmtpConfigured = instance?.config?.is_smtp_configured;
|
|
|
|
|
|
|
|
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]);
|
|
|
|
|
2024-05-01 06:36:51 +00:00
|
|
|
const redirectToUniqueCodeSignIn = async () => {
|
2024-04-29 06:42:33 +00:00
|
|
|
handleStepChange(EAuthSteps.UNIQUE_CODE);
|
|
|
|
};
|
|
|
|
|
|
|
|
const passwordSupport =
|
|
|
|
mode === EAuthModes.SIGN_IN ? (
|
|
|
|
<div className="mt-2 w-full pb-3">
|
|
|
|
{isSmtpConfigured ? (
|
|
|
|
<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>
|
|
|
|
) : (
|
|
|
|
isPasswordInputFocused && <PasswordStrengthMeter password={passwordFormData.password} />
|
|
|
|
);
|
|
|
|
|
|
|
|
const isButtonDisabled = useMemo(
|
|
|
|
() =>
|
2024-05-01 20:49:18 +00:00
|
|
|
!isSubmitting &&
|
2024-04-29 06:42:33 +00:00
|
|
|
!!passwordFormData.password &&
|
|
|
|
(mode === EAuthModes.SIGN_UP
|
|
|
|
? getPasswordStrength(passwordFormData.password) >= 3 &&
|
|
|
|
passwordFormData.password === passwordFormData.confirm_password
|
|
|
|
: true)
|
|
|
|
? false
|
|
|
|
: true,
|
2024-05-01 20:49:18 +00:00
|
|
|
[isSubmitting, mode, passwordFormData.confirm_password, passwordFormData.password]
|
2024-04-29 06:42:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2024-04-30 14:00:42 +00:00
|
|
|
<form
|
2024-05-02 07:44:02 +00:00
|
|
|
className="mt-5 space-y-4"
|
2024-04-30 14:00:42 +00:00
|
|
|
method="POST"
|
|
|
|
action={`${API_BASE_URL}/auth/${mode === EAuthModes.SIGN_IN ? "sign-in" : "sign-up"}/`}
|
2024-05-01 20:49:18 +00:00
|
|
|
onSubmit={() => setIsSubmitting(true)}
|
|
|
|
onError={() => setIsSubmitting(false)}
|
2024-04-30 14:00:42 +00:00
|
|
|
>
|
|
|
|
<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={passwordFormData.email}
|
|
|
|
onChange={(e) => handleFormChange("email", e.target.value)}
|
|
|
|
// hasError={Boolean(errors.email)}
|
|
|
|
placeholder="name@company.com"
|
|
|
|
className="h-[46px] w-full border border-onboarding-border-100 pr-12 placeholder:text-onboarding-text-400"
|
|
|
|
/>
|
|
|
|
{passwordFormData.email.length > 0 && (
|
|
|
|
<XCircle
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
|
|
onClick={handleEmailClear}
|
2024-04-29 06:42:33 +00:00
|
|
|
/>
|
2024-04-30 14:00:42 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<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
|
|
|
|
type={showPassword ? "text" : "password"}
|
|
|
|
name="password"
|
|
|
|
value={passwordFormData.password}
|
|
|
|
onChange={(e) => handleFormChange("password", e.target.value)}
|
|
|
|
placeholder="Enter password"
|
|
|
|
className="h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 placeholder:text-onboarding-text-400"
|
|
|
|
onFocus={() => setIsPasswordInputFocused(true)}
|
|
|
|
onBlur={() => setIsPasswordInputFocused(false)}
|
|
|
|
autoFocus
|
|
|
|
/>
|
|
|
|
{showPassword ? (
|
|
|
|
<EyeOff
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
|
|
onClick={() => setShowPassword(false)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Eye
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
|
|
onClick={() => setShowPassword(true)}
|
|
|
|
/>
|
|
|
|
)}
|
2024-04-29 06:42:33 +00:00
|
|
|
</div>
|
2024-04-30 14:00:42 +00:00
|
|
|
{passwordSupport}
|
|
|
|
</div>
|
|
|
|
{mode === EAuthModes.SIGN_UP && (
|
2024-04-29 06:42:33 +00:00
|
|
|
<div className="space-y-1">
|
2024-04-30 14:00:42 +00:00
|
|
|
<label className="text-sm text-onboarding-text-300 font-medium" htmlFor="confirm_password">
|
|
|
|
Confirm password
|
2024-04-29 06:42:33 +00:00
|
|
|
</label>
|
|
|
|
<div className="relative flex items-center rounded-md bg-onboarding-background-200">
|
|
|
|
<Input
|
|
|
|
type={showPassword ? "text" : "password"}
|
2024-04-30 14:00:42 +00:00
|
|
|
name="confirm_password"
|
|
|
|
value={passwordFormData.confirm_password}
|
|
|
|
onChange={(e) => handleFormChange("confirm_password", e.target.value)}
|
|
|
|
placeholder="Confirm password"
|
2024-04-29 06:42:33 +00:00
|
|
|
className="h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 placeholder:text-onboarding-text-400"
|
|
|
|
/>
|
|
|
|
{showPassword ? (
|
|
|
|
<EyeOff
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
|
|
onClick={() => setShowPassword(false)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Eye
|
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
|
|
|
onClick={() => setShowPassword(true)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
2024-04-30 14:00:42 +00:00
|
|
|
{!!passwordFormData.confirm_password && passwordFormData.password !== passwordFormData.confirm_password && (
|
|
|
|
<span className="text-sm text-red-500">Passwords don{"'"}t match</span>
|
|
|
|
)}
|
2024-04-29 06:42:33 +00:00
|
|
|
</div>
|
2024-04-30 14:00:42 +00:00
|
|
|
)}
|
|
|
|
<div className="space-y-2.5">
|
|
|
|
{mode === EAuthModes.SIGN_IN ? (
|
|
|
|
<>
|
2024-04-29 06:42:33 +00:00
|
|
|
<Button type="submit" variant="primary" className="w-full" size="lg" disabled={isButtonDisabled}>
|
2024-05-01 20:49:18 +00:00
|
|
|
{isSubmitting ? (
|
|
|
|
<Spinner height="20px" width="20px" />
|
|
|
|
) : isSmtpConfigured ? (
|
|
|
|
"Continue"
|
|
|
|
) : (
|
|
|
|
"Go to workspace"
|
|
|
|
)}
|
2024-04-29 06:42:33 +00:00
|
|
|
</Button>
|
2024-04-30 14:00:42 +00:00
|
|
|
{instance && isSmtpConfigured && (
|
|
|
|
<Button
|
|
|
|
type="button"
|
2024-05-01 06:43:36 +00:00
|
|
|
onClick={redirectToUniqueCodeSignIn}
|
2024-04-30 14:00:42 +00:00
|
|
|
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}>
|
2024-05-01 20:49:18 +00:00
|
|
|
{isSubmitting ? <Spinner height="20px" width="20px" /> : "Create account"}
|
2024-04-30 14:00:42 +00:00
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</form>
|
2024-04-29 06:42:33 +00:00
|
|
|
);
|
|
|
|
});
|