Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

13.7 KB
Raw Blame
import { ErrorCode, isNil } from '@activepieces/core-utils';
import {
  OtpType,
  ApEdition,
  ApFlagId,
  TelemetryEventName,
} from '@activepieces/shared';
import { t } from 'i18next';
import { Eye, EyeOff } from 'lucide-react';
import { useCallback, useMemo, useState } from 'react';
import { SubmitHandler, useForm } from 'react-hook-form';
import { useNavigate, useSearchParams } from 'react-router-dom';

import { useTelemetry } from '@/components/providers/telemetry-provider';
import { Button } from '@/components/ui/button';
import { Checkbox } from '@/components/ui/checkbox';
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormMessage,
} from '@/components/ui/form';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import {
  Popover,
  PopoverAnchor,
  PopoverContent,
  PopoverHeader,
  PopoverTitle,
} from '@/components/ui/popover';
import { CheckEmailNote } from '@/features/authentication/components/check-email-note';
import {
  PasswordRequirementsList,
  PasswordStrengthBolt,
} from '@/features/authentication/components/password-validator';
import { flagsHooks } from '@/hooks/flags-hooks';
import { acquisitionUtils } from '@/lib/acquisition-utils';
import { api } from '@/lib/api';
import { authenticationSession } from '@/lib/authentication-session';
import { formatUtils } from '@/lib/format-utils';
import {
  FROM_QUERY_PARAM,
  pendingRedirect,
  useRedirectAfterLogin,
} from '@/lib/navigation-utils';

import { authMutations } from '../hooks/auth-hooks';
import { captchaUtils } from '../utils/captcha-utils';
import { passwordValidation } from '../utils/password-validation-utils';

import {
  TurnstileWidget,
  useTurnstileSiteKey,
} from './auth-landing/turnstile-widget';

const SignUpForm = ({
  showCheckYourEmailNote,
  setShowCheckYourEmailNote,
}: {
  showCheckYourEmailNote: boolean;
  setShowCheckYourEmailNote: (value: boolean) => void;
}) => {
  const [searchParams] = useSearchParams();
  const [showPassword, setShowPassword] = useState(false);
  const [isPasswordFocused, setIsPasswordFocused] = useState(false);

  const form = useForm<SignUpSchema>({
    defaultValues: {
      newsLetter: false,
      password: '',
      email: searchParams.get('email') || '',
    },
  });
  const websiteName = flagsHooks.useWebsiteBranding()?.websiteName;
  const { data: edition } = flagsHooks.useFlag<ApEdition>(ApFlagId.EDITION);
  const showNewsLetterCheckbox = useMemo(() => {
    if (!edition || !websiteName) {
      return false;
    }
    switch (edition) {
      case ApEdition.CLOUD: {
        if (
          typeof websiteName === 'string' &&
          websiteName.toLowerCase() === 'activepieces'
        ) {
          form.setValue('newsLetter', true);
          return true;
        }
        return false;
      }
      case ApEdition.ENTERPRISE:
        return false;
      case ApEdition.COMMUNITY: {
        form.setValue('newsLetter', true);
        return true;
      }
    }
  }, [edition, websiteName]);

  const redirectAfterLogin = useRedirectAfterLogin();
  const navigate = useNavigate();
  const { capture } = useTelemetry();
  const askToCheckEmail = () => {
    pendingRedirect.remember(searchParams.get(FROM_QUERY_PARAM));
    setShowCheckYourEmailNote(true);
  };

  const { mutate, isPending } = authMutations.useSignUp({
    onSuccess: (data) => {
      if (data.verified) {
        authenticationSession.saveResponse(data, false);

        if (isNil(data.platformId)) {
          navigate('/create-platform');
          return;
        }
        redirectAfterLogin();
      } else {
        askToCheckEmail();
      }
    },
    onError: (error) => {
      // The challenge token is single-use, so a refused attempt must be given a
      // fresh one or every retry replays a token Cloudflare has already spent.
      setCaptchaReset((count) => count + 1);
      if (api.isError(error)) {
        const errorCode: ErrorCode | undefined = (
          error.response?.data as { code: ErrorCode }
        )?.code;
        capture({
          name: TelemetryEventName.SIGN_UP_FAILED,
          payload: { errorCode: errorCode ?? 'UNKNOWN' },
        });
        if (captchaUtils.isRejection(error)) {
          form.setError('root.serverError', {
            message: t('That verification expired. Please try again.'),
          });
          return;
        }
        if (isNil(errorCode)) {
          form.setError('root.serverError', {
            message: t('Something went wrong, please try again later'),
          });
          return;
        }
        switch (errorCode) {
          case ErrorCode.EMAIL_IS_NOT_VERIFIED: {
            askToCheckEmail();
            break;
          }
          case ErrorCode.INVITATION_ONLY_SIGN_UP: {
            form.setError('root.serverError', {
              message: t(
                'Sign up is restricted. You need an invitation to join. Please contact the administrator.',
              ),
            });
            break;
          }
          case ErrorCode.EXISTING_USER: {
            form.setError('root.serverError', {
              message: t('Email is already used'),
            });
            break;
          }
          case ErrorCode.EMAIL_AUTH_DISABLED: {
            form.setError('root.serverError', {
              message: t('Email authentication is disabled'),
            });
            break;
          }
          case ErrorCode.DOMAIN_NOT_ALLOWED: {
            form.setError('root.serverError', {
              message: t('Email domain is disallowed'),
            });
            break;
          }
          default: {
            form.setError('root.serverError', {
              message: t('Something went wrong, please try again later'),
            });
            break;
          }
        }
      }
    },
  });

  const [captchaToken, setCaptchaToken] = useState<string | undefined>();
  const [captchaUnavailable, setCaptchaUnavailable] = useState(false);
  const [captchaReset, setCaptchaReset] = useState(0);
  const handleCaptchaUnavailable = useCallback(() => {
    setCaptchaUnavailable(true);
    capture({
      name: TelemetryEventName.CAPTCHA_UNAVAILABLE,
      payload: { surface: 'password-sign-up' },
    });
  }, [capture]);
  const captchaRequired = !isNil(useTurnstileSiteKey()) && !captchaUnavailable;

  const onSubmit: SubmitHandler<SignUpSchema> = (data) => {
    form.setError('root.serverError', {
      message: undefined,
    });
    capture({
      name: TelemetryEventName.SIGN_UP_SUBMITTED,
      payload: {
        method: 'password',
        ...acquisitionUtils.getAcquisitionParams(),
      },
    });
    mutate({
      ...data,
      email: data.email.trim().toLowerCase(),
      trackEvents: true,
      captchaToken,
      attribution: acquisitionUtils.getAcquisitionParams(),
    });
  };

  return showCheckYourEmailNote ? (
    <div className="pt-6">
      <CheckEmailNote
        email={form.getValues().email.trim().toLowerCase()}
        type={OtpType.EMAIL_VERIFICATION}
      />
    </div>
  ) : (
    <>
      <Form {...form}>
        <form className="flex flex-col space-y-4">
          <div className={'flex flex-row gap-2'}>
            <FormField
              control={form.control}
              name="firstName"
              rules={{
                required: t('First name is required'),
              }}
              render={({ field }) => (
                <FormItem className="w-full">
                  <Label htmlFor="firstName">{t('First Name')}</Label>
                  <Input
                    {...field}
                    required
                    id="firstName"
                    type="text"
                    placeholder={'John'}
                    data-testid="sign-up-first-name"
                  />
                  <FormMessage />
                </FormItem>
              )}
            />
            <FormField
              control={form.control}
              name="lastName"
              rules={{
                required: t('Last name is required'),
              }}
              render={({ field }) => (
                <FormItem className="w-full">
                  <Label htmlFor="lastName">{t('Last Name')}</Label>
                  <Input
                    {...field}
                    required
                    id="lastName"
                    type="text"
                    placeholder={'Doe'}
                    data-testid="sign-up-last-name"
                  />
                  <FormMessage />
                </FormItem>
              )}
            />
          </div>
          <FormField
            control={form.control}
            name="email"
            rules={{
              required: t('Email is required'),
              validate: (email: string) =>
                formatUtils.emailRegex.test(email) || t('Email is invalid'),
            }}
            render={({ field }) => (
              <FormItem className="grid space-y-1">
                <Label htmlFor="email">{t('Email')}</Label>
                <Input
                  {...field}
                  required
                  id="email"
                  type="email"
                  placeholder={'[email protected]'}
                  data-testid="sign-up-email"
                />
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="password"
            rules={{
              required: t('Password is required'),
              validate: passwordValidation,
            }}
            render={({ field }) => (
              <FormItem className="grid space-y-1">
                <Label htmlFor="password">{t('Password')}</Label>
                <Popover open={isPasswordFocused}>
                  <PopoverAnchor asChild>
                    <div className="relative flex items-center">
                      <Input
                        {...field}
                        required
                        id="password"
                        type={showPassword ? 'text' : 'password'}
                        placeholder={'********'}
                        className="pr-16"
                        data-testid="sign-up-password"
                        onFocus={() => setIsPasswordFocused(true)}
                        onBlur={() => setIsPasswordFocused(false)}
                      />
                      <div className="absolute right-1 flex items-center gap-0.5">
                        <PasswordStrengthBolt password={field.value ?? ''} />
                        <Button
                          type="button"
                          variant="ghost"
                          size="icon-xs"
                          tabIndex={-1}
                          onClick={() => setShowPassword((v) => !v)}
                          className="text-gray-11 hover:text-gray-12"
                        >
                          {showPassword ? (
                            <EyeOff className="w-4 h-4" />
                          ) : (
                            <Eye className="w-4 h-4" />
                          )}
                        </Button>
                      </div>
                    </div>
                  </PopoverAnchor>
                  <PopoverContent
                    side="right"
                    align="center"
                    sideOffset={6}
                    onOpenAutoFocus={(e) => e.preventDefault()}
                    className="w-auto shadow-none"
                  >
                    <div className="absolute -left-[4.5px] top-1/2 -translate-y-1/2">
                      <div className="w-2.5 h-2.5 rotate-45 bg-panel border-l border-b border-gray-6" />
                    </div>
                    <PopoverHeader className="mb-2">
                      <PopoverTitle className="text-xs">
                        {t('Password Requirements')}
                      </PopoverTitle>
                    </PopoverHeader>
                    <PasswordRequirementsList
                      password={field.value ?? ''}
                      isSubmitted={form.formState.submitCount > 0}
                    />
                  </PopoverContent>
                </Popover>
                <FormMessage />
              </FormItem>
            )}
          />
          {showNewsLetterCheckbox && (
            <FormField
              control={form.control}
              name="newsLetter"
              render={({ field }) => (
                <FormItem className="flex items-center gap-2 ">
                  <FormControl>
                    <Checkbox
                      id="newsLetter"
                      className="m-0!"
                      checked={field.value}
                      onCheckedChange={field.onChange}
                    ></Checkbox>
                  </FormControl>
                  <Label htmlFor="newsLetter" className="text-xs">
                    {t(`Get emails about updates and newsletters`)}
                  </Label>
                  <FormMessage />
                </FormItem>
              )}
            />
          )}

          {form?.formState?.errors?.root?.serverError && (
            <FormMessage>
              {form.formState.errors.root.serverError.message}
            </FormMessage>
          )}
          <TurnstileWidget
            onToken={setCaptchaToken}
            onUnavailable={handleCaptchaUnavailable}
            resetSignal={captchaReset}
          />
          <Button
            loading={isPending}
            disabled={captchaRequired && isNil(captchaToken)}
            onClick={(e) => form.handleSubmit(onSubmit)(e)}
            data-testid="sign-up-button"
          >
            {t('Sign up')}
          </Button>
        </form>
      </Form>
    </>
  );
};

SignUpForm.displayName = 'SignUp';

export { SignUpForm };

type SignUpSchema = {
  email: string;
  firstName: string;
  lastName: string;
  password: string;
  newsLetter: boolean;
};