import { FC, useState } from 'react' import ReactMarkdown from 'react-markdown' import * as Tooltip from '@radix-ui/react-tooltip' import { PermissionAction } from '@supabase/shared-types/out/constants' import { Alert, Button, Collapsible, Form, IconCheck, IconChevronUp, Input } from 'ui' import { useStore, checkPermissions } from 'hooks' import { BASE_PATH } from 'lib/constants' import { Provider } from './AuthProvidersForm.types' import { ProviderCollapsibleClasses } from './AuthProvidersForm.constants' import FormField from './FormField' import { useCustomDomainsQuery } from 'data/custom-domains/custom-domains-query' import { useParams } from 'common' interface Props { provider: Provider } const ProviderForm: FC = ({ provider }) => { const [open, setOpen] = useState(false) const { authConfig, ui } = useStore() const { ref } = useParams() const doubleNegativeKeys = ['MAILER_AUTOCONFIRM', 'SMS_AUTOCONFIRM'] const canUpdateConfig = checkPermissions(PermissionAction.UPDATE, 'custom_config_gotrue') const { data: customDomainData } = useCustomDomainsQuery({ projectRef: ref }) const generateInitialValues = () => { const initialValues: { [x: string]: string } = {} Object.keys(provider.properties).forEach((key) => { // When the key is a 'double negative' key, we must reverse the boolean before adding it to the form const isDoubleNegative = doubleNegativeKeys.includes(key) if (provider.title === 'SAML 2.0') { initialValues[key] = authConfig.config[key] ?? false } else { initialValues[key] = isDoubleNegative ? !authConfig.config[key] : authConfig.config[key] ?? '' } }) return initialValues } // [Joshen] Doing this check as SAML doesn't follow the same naming structure as the other provider options const isActive = provider.title === 'SAML 2.0' ? authConfig.config['SAML_ENABLED'] || false : authConfig.config[`EXTERNAL_${provider?.title?.toUpperCase()}_ENABLED`] const INITIAL_VALUES = generateInitialValues() const onSubmit = async (values: any, { setSubmitting, resetForm }: any) => { const payload = { ...values } // When the key is a 'double negative' key, we must reverse the boolean before the payload can be sent Object.keys(values).map((x: string) => { if (doubleNegativeKeys.includes(x)) { payload[x] = !values[x] } }) const { error } = await authConfig.update(payload) if (!error) { resetForm({ values: { ...values }, initialValues: { ...values } }) setOpen(false) ui.setNotification({ category: 'success', message: 'Successfully updated settings' }) } else { ui.setNotification({ error, category: 'error', message: `Failed to update settings: ${error?.message}`, }) } setSubmitting(false) } return (
{({ isSubmitting, handleReset, initialValues, values }: any) => { const noChanges = JSON.stringify(initialValues) === JSON.stringify(values) return (
{Object.keys(provider.properties).map((x: string) => ( ))} {provider?.misc?.alert && ( {provider.misc.alert.description} )} {provider.misc.requiresRedirect && ( <> {provider.misc.helper} )}
{!canUpdateConfig && (
You need additional permissions to update provider settings
)}
) }}
) } export default ProviderForm