import { PermissionAction } from '@supabase/shared-types/out/constants' import { ExternalLink } from 'lucide-react' import Link from 'next/link' import { useEffect } from 'react' import { toast } from 'sonner' import { boolean, object, string } from 'yup' import { useParams } from 'common' import { Markdown } from 'components/interfaces/Markdown' import { FormActions } from 'components/ui/Forms/FormActions' import { FormPanel } from 'components/ui/Forms/FormPanel' import { FormSection, FormSectionContent, FormSectionLabel } from 'components/ui/Forms/FormSection' import NoPermission from 'components/ui/NoPermission' import { useAuthConfigQuery } from 'data/auth/auth-config-query' import { useAuthConfigUpdateMutation } from 'data/auth/auth-config-update-mutation' import { useCheckPermissions } from 'hooks/misc/useCheckPermissions' import { AlertDescription_Shadcn_, AlertTitle_Shadcn_, Alert_Shadcn_, Button, Form, Toggle, WarningIcon, } from 'ui' import { FormHeader } from 'components/ui/Forms/FormHeader' // Use a const string to represent no chars option. Represented as empty string on the backend side. const NO_REQUIRED_CHARACTERS = 'NO_REQUIRED_CHARS' const schema = object({ DISABLE_SIGNUP: boolean().required(), EXTERNAL_ANONYMOUS_USERS_ENABLED: boolean().required(), SECURITY_MANUAL_LINKING_ENABLED: boolean().required(), SITE_URL: string().required('Must have a Site URL'), }) const formId = 'auth-config-basic-settings' const BasicAuthSettingsForm = () => { const { ref: projectRef } = useParams() const { data: authConfig, error: authConfigError, isLoading, isError, isSuccess, } = useAuthConfigQuery({ projectRef }) const { mutate: updateAuthConfig, isLoading: isUpdatingConfig } = useAuthConfigUpdateMutation() const canReadConfig = useCheckPermissions(PermissionAction.READ, 'custom_config_gotrue') const canUpdateConfig = useCheckPermissions(PermissionAction.UPDATE, 'custom_config_gotrue') const INITIAL_VALUES = { DISABLE_SIGNUP: !authConfig?.DISABLE_SIGNUP, EXTERNAL_ANONYMOUS_USERS_ENABLED: authConfig?.EXTERNAL_ANONYMOUS_USERS_ENABLED, SECURITY_MANUAL_LINKING_ENABLED: authConfig?.SECURITY_MANUAL_LINKING_ENABLED || false, SITE_URL: authConfig?.SITE_URL, } const onSubmit = (values: any, { resetForm }: any) => { const payload = { ...values } payload.DISABLE_SIGNUP = !values.DISABLE_SIGNUP // 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 }, { onError: (error) => { toast.error(`Failed to update settings: ${error?.message}`) }, onSuccess: () => { toast.success(`Successfully updated settings`) resetForm({ values: values, initialValues: values }) }, } ) } if (isError) { return ( Failed to retrieve auth configuration {authConfigError.message} ) } if (!canReadConfig) { return } return (
{({ handleReset, resetForm, values, initialValues, setFieldValue }: any) => { const hasChanges = JSON.stringify(values) !== JSON.stringify(initialValues) // Form is reset once remote data is loaded in store // eslint-disable-next-line react-hooks/rules-of-hooks useEffect(() => { if (isSuccess) resetForm({ values: INITIAL_VALUES, initialValues: INITIAL_VALUES }) }, [isSuccess]) return ( <>
} > User Signups}> } disabled={!canUpdateConfig} /> } disabled={!canUpdateConfig} /> {values.EXTERNAL_ANONYMOUS_USERS_ENABLED && (
Anonymous users will use the{' '} authenticated role when signing in

As a result, anonymous users will be subjected to RLS policies that apply to the public and{' '} authenticated roles. We strongly advise{' '} reviewing your RLS policies {' '} to ensure that access to your data is restricted where required.

)} {!authConfig?.SECURITY_CAPTCHA_ENABLED && values.EXTERNAL_ANONYMOUS_USERS_ENABLED && ( We highly recommend{' '} { const el = document.getElementById('enable-captcha') if (el) el.scrollIntoView({ behavior: 'smooth', block: 'center' }) }} > enabling captcha {' '} for anonymous sign-ins This will prevent potential abuse on sign-ins which may bloat your database and incur costs for monthly active users (MAU) )}
) }} ) } export default BasicAuthSettingsForm