Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

8.5 KB
Raw Blame
import HCaptcha from '@hcaptcha/react-hcaptcha'
import { zodResolver } from '@hookform/resolvers/zod'
import { EyeIcon, EyeOffIcon } from 'lucide-react'
import { motion } from 'motion/react'
import { useRouter } from 'next/router'
import { parseAsString, useQueryStates } from 'nuqs'
import { useRef, useState } from 'react'
import { SubmitHandler, useForm, useWatch } from 'react-hook-form'
import { toast } from 'sonner'
import { Button, cn, Form, FormControl, FormField, Input } from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import z from 'zod'

import PasswordConditionsHelper from './PasswordConditionsHelper'
import { useSignUpMutation } from '@/data/misc/signup-mutation'
import { BASE_PATH } from '@/lib/constants'
import { buildSignUpReturnPath } from '@/lib/gotrue'
import { classifyApiError, classifyValidationError } from '@/lib/telemetry/funnel-errors'
import { useTrackFunnelError } from '@/lib/telemetry/use-track-funnel-error'

const schema = z.object({
  email: z.string().min(1, 'Email is required').email('Must be a valid email'),
  password: z
    .string()
    .min(1, 'Password is required')
    .max(72, 'Password cannot exceed 72 characters')
    .refine((password) => password.length >= 8, 'Password must be at least 8 characters')
    .refine(
      (password) => /[A-Z]/.test(password),
      'Password must contain at least 1 uppercase character'
    )
    .refine(
      (password) => /[a-z]/.test(password),
      'Password must contain at least 1 lowercase character'
    )
    .refine((password) => /[0-9]/.test(password), 'Password must contain at least 1 number')
    .refine(
      (password) => /[!@#$%^&*()_+\-=\[\]{};`':"\\|,.<>\/?]/.test(password),
      'Password must contain at least 1 symbol'
    ),
})

const formId = 'sign-up-form'

export const SignUpForm = ({ onSuccess }: { onSuccess?: () => void }) => {
  const captchaRef = useRef<HCaptcha>(null)
  const [showConditions, setShowConditions] = useState(false)
  const [isSubmitted, setIsSubmitted] = useState(false)
  const [passwordHidden, setPasswordHidden] = useState(true)
  const [captchaToken, setCaptchaToken] = useState<string | null>(null)
  const router = useRouter()
  const form = useForm<z.infer<typeof schema>>({
    resolver: zodResolver(schema),
    defaultValues: { email: '', password: '' },
  })

  const [searchParams] = useQueryStates({
    auth_id: parseAsString.withDefault(''),
    token: parseAsString.withDefault(''),
    organization_slug: parseAsString.withDefault(''),
  })

  const trackFunnelError = useTrackFunnelError()

  const { mutate: signup, isPending: isSigningUp } = useSignUpMutation({
    onSuccess: () => {
      toast.success(`Signed up successfully!`)
      setIsSubmitted(true)
      onSuccess?.()
    },
    onError: (error) => {
      setCaptchaToken(null)
      captchaRef.current?.resetCaptcha()
      const toastId = toast.error(`Failed to sign up: ${error.message}`)
      trackFunnelError('signup', classifyApiError('signup', error), 'toast', toastId)
    },
  })

  const onSubmit: SubmitHandler<z.infer<typeof schema>> = async ({ email, password }) => {
    // [Joshen] Separate submitting state as there's 2 async processes here
    let token = captchaToken
    if (!token) {
      const captchaResponse = await captchaRef.current?.execute({ async: true })
      token = captchaResponse?.response ?? null
    }

    const isInsideOAuthFlow = !!searchParams.auth_id
    const redirectUrlBase = `${
      process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview'
        ? location.origin
        : process.env.NEXT_PUBLIC_SITE_URL
    }${BASE_PATH}`

    let redirectTo: string

    if (isInsideOAuthFlow) {
      const authorizeParams = new URLSearchParams({ auth_id: searchParams.auth_id })
      if (searchParams.token) authorizeParams.set('token', searchParams.token)
      if (searchParams.organization_slug) {
        authorizeParams.set('organization_slug', searchParams.organization_slug)
      }
      redirectTo = `${redirectUrlBase}/authorize?${authorizeParams.toString()}`
    } else {
      // Build the post-verification return path from returnTo and other query params.
      const { returnTo } = router.query
      const fullPath = buildSignUpReturnPath(returnTo)
      const fullRedirectUrl = `${redirectUrlBase}${fullPath}`
      redirectTo = fullRedirectUrl
    }

    signup({
      email,
      password,
      hcaptchaToken: token ?? null,
      redirectTo,
    })
  }

  const password = useWatch({ control: form.control, name: 'password' })
  const isSubmitting = form.formState.isSubmitting || isSigningUp

  return (
    <div className="relative">
      {isSubmitted && (
        <motion.div
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          transition={{ duration: 0.5, delay: 0.3 }}
          className="w-full"
        >
          <Admonition
            type="success"
            title="Check your email"
            description="We sent you a link to finish signing up. It expires in 10 minutes."
          />
        </motion.div>
      )}
      <div
        inert={isSubmitted || undefined}
        className={cn(
          'w-full transition-all duration-500',
          isSubmitted
            ? 'max-h-0 overflow-hidden opacity-0 pointer-events-none py-0'
            : 'max-h-[1000px] opacity-100'
        )}
      >
        <Form {...form}>
          <form
            id={formId}
            method="POST"
            className="flex flex-col gap-4"
            onSubmit={form.handleSubmit(onSubmit, (errors) =>
              trackFunnelError('signup', classifyValidationError('signup', errors), 'form')
            )}
          >
            <FormField
              key="email"
              name="email"
              control={form.control}
              render={({ field }) => (
                <FormItemLayout label="Email">
                  <FormControl>
                    <Input
                      autoComplete="email"
                      disabled={isSubmitting}
                      {...field}
                      placeholder="[email protected]"
                    />
                  </FormControl>
                </FormItemLayout>
              )}
            />

            <FormField
              key="password"
              name="password"
              control={form.control}
              render={({ field }) => (
                <FormItemLayout label="Password">
                  <div className="relative">
                    <FormControl>
                      <Input
                        type={passwordHidden ? 'password' : 'text'}
                        autoComplete="new-password"
                        placeholder="&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;"
                        {...field}
                        onFocus={() => setShowConditions(true)}
                        disabled={isSubmitting}
                      />
                    </FormControl>
                    <Button
                      title={passwordHidden ? `Show password` : `Hide password`}
                      aria-label={passwordHidden ? `Show password` : `Hide password`}
                      className="absolute right-1 top-1 px-1.5"
                      icon={passwordHidden ? <EyeIcon /> : <EyeOffIcon />}
                      disabled={isSubmitting}
                      onClick={() => setPasswordHidden((prev) => !prev)}
                    />
                  </div>
                </FormItemLayout>
              )}
            />

            {showConditions && (
              <motion.div
                initial={{ maxHeight: '0px' }}
                animate={{ maxHeight: '500px' }}
                transition={{ duration: 0.8, delay: 0 }}
                className="overflow-y-hidden"
              >
                <PasswordConditionsHelper password={password} />
              </motion.div>
            )}

            <div className="self-center">
              <HCaptcha
                ref={captchaRef}
                sitekey={process.env.NEXT_PUBLIC_HCAPTCHA_SITE_KEY!}
                size="invisible"
                onVerify={(token) => setCaptchaToken(token)}
                onExpire={() => setCaptchaToken(null)}
              />
            </div>

            <Button
              variant="primary"
              block
              form={formId}
              type="submit"
              size="large"
              disabled={password.length === 0 || isSubmitting}
              loading={isSubmitting}
            >
              Sign up
            </Button>
          </form>
        </Form>
      </div>
    </div>
  )
}