Skip to content

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

10.5 KB
Raw Blame
import { zodResolver } from '@hookform/resolvers/zod'
import { useParams } from 'common'
import { useTheme } from 'next-themes'
import { useQueryState } from 'nuqs'
import { useCallback, useEffect, useId, useMemo, useState } from 'react'
import { useForm } from 'react-hook-form'
import ReactMarkdown from 'react-markdown'
import { toast } from 'sonner'
import {
  Badge,
  Button,
  Form,
  Sheet,
  SheetContent,
  SheetFooter,
  SheetHeader,
  SheetSection,
  SheetTitle,
} from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import { Input } from 'ui-patterns/DataInputs/Input'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'

import { NO_REQUIRED_CHARACTERS } from '../Auth.constants'
import { AuthAlert } from './AuthAlert'
import type { Provider } from './AuthProvidersForm.types'
import FormField from './FormField'
import { Markdown } from '@/components/interfaces/Markdown'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { DocsButton } from '@/components/ui/DocsButton'
import { ResourceItem } from '@/components/ui/Resource/ResourceItem'
import type { components } from '@/data/api'
import { useAuthConfigUpdateMutation } from '@/data/auth/auth-config-update-mutation'
import { useProjectApiUrl } from '@/data/config/project-endpoint-query'
import { useHasEntitlementAccess } from '@/hooks/misc/useCheckEntitlements'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
import { BASE_PATH } from '@/lib/constants'

interface ProviderFormProps {
  config: components['schemas']['GoTrueConfigResponse']
  provider: Provider
  isActive: boolean
}

const doubleNegativeKeys = ['SMS_AUTOCONFIRM']

export const ProviderForm = ({ config, provider, isActive }: ProviderFormProps) => {
  const { resolvedTheme } = useTheme()
  const { ref: projectRef } = useParams()
  const { data: organization } = useSelectedOrganizationQuery()
  const [urlProvider, setUrlProvider] = useQueryState('provider', { defaultValue: '' })

  const [open, setOpen] = useState(false)
  const { mutate: updateAuthConfig, isPending: isUpdatingConfig } = useAuthConfigUpdateMutation()

  const { data: endpoint } = useProjectApiUrl({ projectRef })

  const { can: canUpdateConfig } = useAsyncCheckPermissionsV2(
    FGA_PERMISSIONS.PROJECT.AUTH_CONFIG_WRITE
  )

  const shouldDisableField = (field: string): boolean => {
    const shouldDisableSmsFields =
      config.HOOK_SEND_SMS_ENABLED &&
      field.startsWith('SMS_') &&
      ![
        'SMS_AUTOCONFIRM',
        'SMS_OTP_EXP',
        'SMS_OTP_LENGTH',
        'SMS_OTP_LENGTH',
        'SMS_TEMPLATE',
        'SMS_TEST_OTP',
        'SMS_TEST_OTP_VALID_UNTIL',
      ].includes(field)
    return (
      ['EXTERNAL_SLACK_CLIENT_ID', 'EXTERNAL_SLACK_SECRET'].includes(field) ||
      shouldDisableSmsFields
    )
  }

  const hasEntitlementAccess = useHasEntitlementAccess()

  const getValuesForProvider = useCallback(
    (config: components['schemas']['GoTrueConfigResponse']) => {
      const values: { [x: string]: string | boolean } = {}
      Object.keys(provider.properties).forEach((key) => {
        // This ensures the default value is visibly selected
        if (key === 'PASSWORD_REQUIRED_CHARACTERS' && config.PASSWORD_REQUIRED_CHARACTERS === '') {
          values[key] = NO_REQUIRED_CHARACTERS
          return
        }

        const isDoubleNegative = doubleNegativeKeys.includes(key)
        if (provider.title === 'SAML 2.0') {
          const configValue = (config as any)[key]
          values[key] = configValue || (provider.properties[key].type === 'boolean' ? false : '')
        } else {
          if (isDoubleNegative) {
            values[key] = !(config as any)[key]
          } else {
            const configValue = (config as any)[key]
            values[key] = configValue
              ? configValue
              : provider.properties[key].type === 'boolean'
                ? false
                : ''
          }
        }
      })
      return values
    },
    [provider]
  )

  const INITIAL_VALUES = useMemo(() => {
    // This check will always be true but let us avoid adding an eslint disable comment on unused memo dependencies
    // which could hide real issues in the future.
    // Adding the provider in the memo dependencies ensures the INITIAL_VALUES is properly applied
    if (!provider) return
    return getValuesForProvider(config)
  }, [config, getValuesForProvider, provider])

  const onSubmit = (values: any) => {
    const payload = { ...values }
    Object.keys(values).map((x: string) => {
      if (doubleNegativeKeys.includes(x)) payload[x] = !values[x]
      if (payload[x] === '') payload[x] = null
    })

    // The backend uses empty string to represent no required characters in the password
    if (payload.PASSWORD_REQUIRED_CHARACTERS === NO_REQUIRED_CHARACTERS) {
      payload.PASSWORD_REQUIRED_CHARACTERS = ''
    }

    updateAuthConfig(
      { projectRef: projectRef!, config: payload },
      {
        onSuccess: (newValues) => {
          setOpen(false)
          setUrlProvider(null)
          form.reset(getValuesForProvider(newValues))
          toast.success('Successfully updated settings')
        },
      }
    )
  }

  // Handle clicking on a provider in the list
  const handleProviderClick = () => setUrlProvider(provider.title)

  const handleOpenChange = (isOpen: boolean) => {
    // Remove provider query param from URL when closed
    if (!isOpen) setUrlProvider(null)
  }

  // Open or close the form based on the query parameter
  useEffect(() => {
    const isProviderInQuery = urlProvider.toLowerCase() === provider.title.toLowerCase()
    setOpen(isProviderInQuery)
  }, [urlProvider, provider.title])

  const form = useForm({
    defaultValues: INITIAL_VALUES,
    resolver: zodResolver(provider.validationSchema),
    shouldUnregister: false,
  })

  useEffect(() => {
    if (open) {
      form.reset(INITIAL_VALUES)
    }
  }, [open, form, INITIAL_VALUES])
  const formId = useId()

  return (
    <>
      <ResourceItem
        onClick={handleProviderClick}
        media={
          <img
            src={`${BASE_PATH}/img/icons/${provider.misc.iconKey}${provider.misc.hasLightIcon && !resolvedTheme?.includes('dark') ? '-light' : ''}.svg`}
            width={18}
            height={18}
            alt={`${provider.title} auth icon`}
          />
        }
        meta={
          <Badge variant={isActive ? 'success' : 'default'}>
            {isActive ? 'Enabled' : 'Disabled'}
          </Badge>
        }
      >
        {provider.title}
      </ResourceItem>

      <Sheet open={open} onOpenChange={handleOpenChange}>
        <SheetContent className="flex flex-col gap-0" size="lg">
          <SheetHeader className="shrink-0 flex items-center gap-4">
            <img
              src={`${BASE_PATH}/img/icons/${provider.misc.iconKey}${provider.misc.hasLightIcon && !resolvedTheme?.includes('dark') ? '-light' : ''}.svg`}
              width={18}
              height={18}
              alt={`${provider.title} auth icon`}
            />
            <SheetTitle>{provider.title}</SheetTitle>
          </SheetHeader>
          <Form {...form}>
            <form
              id={formId}
              name={formId}
              className="overflow-y-auto grow px-0"
              onSubmit={form.handleSubmit(onSubmit)}
            >
              <AuthAlert
                title={provider.title}
                isHookSendSMSEnabled={config.HOOK_SEND_SMS_ENABLED}
              />

              {Object.keys(provider.properties).map((x: string) => {
                const { entitlementKey } = provider.properties[x]
                const hasAccess = entitlementKey == null || hasEntitlementAccess(entitlementKey)

                return (
                  <FormField
                    key={x}
                    projectRef={projectRef}
                    organizationSlug={organization?.slug}
                    name={x}
                    properties={provider.properties[x]}
                    control={form.control}
                    readOnly={shouldDisableField(x) || !canUpdateConfig}
                    hasAccess={hasAccess}
                  />
                )
              })}

              {provider?.misc?.alert && (
                <SheetSection>
                  <Admonition
                    type="warning"
                    title={provider.misc.alert.title}
                    description={<ReactMarkdown>{provider.misc.alert.description}</ReactMarkdown>}
                  />
                </SheetSection>
              )}

              {provider.misc.requiresRedirect && (
                <SheetSection>
                  <FormItemLayout
                    layout="horizontal"
                    label="Callback URL (for OAuth)"
                    description={
                      <Markdown
                        content={provider.misc.helper}
                        className="text-foreground-lighter"
                      />
                    }
                  >
                    <Input copy readOnly value={endpoint ? `${endpoint}/auth/v1/callback` : ''} />
                  </FormItemLayout>
                </SheetSection>
              )}
            </form>
          </Form>
          <SheetFooter className="shrink-0">
            <div className="flex items-center justify-between w-full">
              <DocsButton href={provider.link} />
              <div className="flex items-center gap-x-3">
                <Button
                  type="reset"
                  onClick={() => {
                    setOpen(false)
                    setUrlProvider(null)
                    form.reset()
                  }}
                  disabled={isUpdatingConfig}
                >
                  Cancel
                </Button>
                <ButtonTooltip
                  variant="primary"
                  form={formId}
                  type="submit"
                  loading={isUpdatingConfig}
                  disabled={isUpdatingConfig || !canUpdateConfig || !form.formState.isDirty}
                  tooltip={{
                    content: {
                      side: 'bottom',
                      text: !canUpdateConfig
                        ? 'You need additional permissions to update provider settings'
                        : undefined,
                    },
                  }}
                >
                  Save
                </ButtonTooltip>
              </div>
            </div>
          </SheetFooter>
        </SheetContent>
      </Sheet>
    </>
  )
}