The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
chore: migrate MFA settings form to `zod` (#44751)
Gildas Garciae95f1cc67c6a
diff --git a/apps/studio/components/interfaces/Auth/MfaAuthSettingsForm/MfaAuthSettingsForm.tsx b/apps/studio/components/interfaces/Auth/MfaAuthSettingsForm/MfaAuthSettingsForm.tsxindex 204c14acd39..cd78fb8cb27 100644--- a/apps/studio/components/interfaces/Auth/MfaAuthSettingsForm/MfaAuthSettingsForm.tsx+++ b/apps/studio/components/interfaces/Auth/MfaAuthSettingsForm/MfaAuthSettingsForm.tsx@@ -1,8 +1,8 @@-import { yupResolver } from '@hookform/resolvers/yup'+import { zodResolver } from '@hookform/resolvers/zod'import { PermissionAction } from '@supabase/shared-types/out/constants'import { useParams } from 'common'import { useEffect, useState } from 'react'-import { useForm } from 'react-hook-form'+import { SubmitHandler, useForm } from 'react-hook-form'import { toast } from 'sonner'import {Alert_Shadcn_,@@ -37,7 +37,7 @@ import {PageSectionSummary,PageSectionTitle,} from 'ui-patterns/PageSection'-import { boolean, number, object, string } from 'yup'+import * as z from 'zod'import AlertError from '@/components/ui/AlertError'import NoPermission from '@/components/ui/NoPermission'@@ -75,24 +75,37 @@ const MfaStatusToState = (status: (typeof MFAFactorSelectionOptions)[number]['va: { verifyEnabled: false, enrollEnabled: false }}-const totpSchema = object({- MFA_TOTP: string().required(),- MFA_MAX_ENROLLED_FACTORS: number()- .min(0, 'Must be a value 0 or larger')- .max(30, 'Must be a value no greater than 30'),+const totpSchema = z.object({+ MFA_TOTP: z.string().min(1, 'Required'),+ MFA_MAX_ENROLLED_FACTORS: z.preprocess(+ (val) => (val === '' || val == null ? undefined : val),+ z.coerce+ .number({ required_error: 'Required', invalid_type_error: 'Required' })+ .min(0, 'Must be a value 0 or larger')+ .max(30, 'Must be a value no greater than 30')+ ),})-const phoneSchema = object({- MFA_PHONE: string().required(),- MFA_PHONE_OTP_LENGTH: number()- .min(6, 'Must be a value 6 or larger')- .max(30, 'must be a value no greater than 30'),- MFA_PHONE_TEMPLATE: string().required('SMS template is required.'),+type TotpFormValues = z.infer<typeof totpSchema>++const phoneSchema = z.object({+ MFA_PHONE: z.string().min(1, 'Required'),+ MFA_PHONE_OTP_LENGTH: z.preprocess(+ (val) => (val === '' || val == null ? undefined : val),+ z.coerce+ .number({ required_error: 'Required', invalid_type_error: 'Required' })+ .min(6, 'Must be a value 6 or larger')+ .max(30, 'must be a value no greater than 30')+ ),+ MFA_PHONE_TEMPLATE: z.string().min(1, 'Required'),})-const securitySchema = object({- MFA_ALLOW_LOW_AAL: boolean().required(),+type PhoneFormValues = z.infer<typeof phoneSchema>++const securitySchema = z.object({+ MFA_ALLOW_LOW_AAL: z.boolean({ required_error: 'Required' }),})+type SecurityFormValues = z.infer<typeof securitySchema>export const MfaAuthSettingsForm = () => {const { ref: projectRef } = useParams()@@ -138,34 +151,37 @@ export const MfaAuthSettingsForm = () => {const hasValidMFAPhoneProvider = authConfig?.EXTERNAL_PHONE_ENABLED === trueconst hasValidMFAProvider = hasValidMFAPhoneProvider || sendSMSHookIsEnabled- const totpForm = useForm({- resolver: yupResolver(totpSchema),+ const totpForm = useForm<TotpFormValues>({+ resolver: zodResolver(totpSchema),defaultValues: {MFA_TOTP: 'Enabled',MFA_MAX_ENROLLED_FACTORS: 10,},})+ const { reset: resetTotpForm } = totpForm- const phoneForm = useForm({- resolver: yupResolver(phoneSchema),+ const phoneForm = useForm<PhoneFormValues>({+ resolver: zodResolver(phoneSchema),defaultValues: {MFA_PHONE: 'Disabled',MFA_PHONE_OTP_LENGTH: 6,MFA_PHONE_TEMPLATE: 'Your code is {{ .Code }}',},})+ const { reset: resetPhoneForm } = phoneForm- const securityForm = useForm({- resolver: yupResolver(securitySchema),+ const securityForm = useForm<SecurityFormValues>({+ resolver: zodResolver(securitySchema),defaultValues: {MFA_ALLOW_LOW_AAL: false,},})+ const { reset: resetSecurityForm } = securityFormuseEffect(() => {if (authConfig) {if (!isUpdatingTotpForm) {- totpForm.reset({+ resetTotpForm({MFA_TOTP:determineMFAStatus(authConfig?.MFA_TOTP_VERIFY_ENABLED ?? true,@@ -176,7 +192,7 @@ export const MfaAuthSettingsForm = () => {}if (!isUpdatingPhoneForm) {- phoneForm.reset({+ resetPhoneForm({MFA_PHONE:determineMFAStatus(authConfig?.MFA_PHONE_VERIFY_ENABLED || false,@@ -188,23 +204,30 @@ export const MfaAuthSettingsForm = () => {}if (!isUpdatingSecurityForm) {- securityForm.reset({+ resetSecurityForm({MFA_ALLOW_LOW_AAL: authConfig?.MFA_ALLOW_LOW_AAL ?? true,})}}- }, [authConfig, isUpdatingTotpForm, isUpdatingPhoneForm, isUpdatingSecurityForm])-- const onSubmitTotpForm = (values: any) => {+ }, [+ authConfig,+ isUpdatingTotpForm,+ isUpdatingPhoneForm,+ isUpdatingSecurityForm,+ resetTotpForm,+ resetPhoneForm,+ resetSecurityForm,+ ])++ const onSubmitTotpForm: SubmitHandler<TotpFormValues> = (values) => {const { verifyEnabled: MFA_TOTP_VERIFY_ENABLED, enrollEnabled: MFA_TOTP_ENROLL_ENABLED } =MfaStatusToState(values.MFA_TOTP)const payload = {- ...values,+ MFA_MAX_ENROLLED_FACTORS: values.MFA_MAX_ENROLLED_FACTORS,MFA_TOTP_ENROLL_ENABLED,MFA_TOTP_VERIFY_ENABLED,}- delete payload.MFA_TOTPsetIsUpdatingTotpForm(true)@@ -223,39 +246,40 @@ export const MfaAuthSettingsForm = () => {)}- const onSubmitSecurityForm = (values: any) => {- const payload = { ...values }-+ const onSubmitSecurityForm: SubmitHandler<SecurityFormValues> = (values) => {setIsUpdatingSecurityForm(true)updateAuthConfig(- { projectRef: projectRef!, config: payload },+ { projectRef: projectRef!, config: values },{onError: (error) => {- toast.error(`Failed to update phone MFA settings: ${error?.message}`)+ toast.error(`Failed to update enhanced MFA security settings: ${error?.message}`)setIsUpdatingSecurityForm(false)},onSuccess: () => {- toast.success('Successfully updated phone MFA settings')+ toast.success('Successfully updated enhanced MFA security settings')setIsUpdatingSecurityForm(false)},})}- const onSubmitPhoneForm = (values: any) => {- let payload = { ...values }+ const onSubmitPhoneForm: SubmitHandler<PhoneFormValues> = (values) => {+ let payload: Record<string, string | number | boolean> = {+ MFA_PHONE_OTP_LENGTH: values.MFA_PHONE_OTP_LENGTH,+ MFA_PHONE_TEMPLATE: values.MFA_PHONE_TEMPLATE,+ }if (hasAccessToMFA) {const { verifyEnabled: MFA_PHONE_VERIFY_ENABLED, enrollEnabled: MFA_PHONE_ENROLL_ENABLED } =MfaStatusToState(values.MFA_PHONE)payload = {- ...payload,+ MFA_PHONE_OTP_LENGTH: values.MFA_PHONE_OTP_LENGTH,+ MFA_PHONE_TEMPLATE: values.MFA_PHONE_TEMPLATE,MFA_PHONE_ENROLL_ENABLED,MFA_PHONE_VERIFY_ENABLED,}}- delete payload.MFA_PHONEsetIsUpdatingPhoneForm(true)@@ -380,6 +404,10 @@ export const MfaAuthSettingsForm = () => {max={30}{...field}disabled={!canUpdateConfig}+ data-1p-ignore // 1Password+ data-lpignore="true" // LastPass+ data-form-type="other" // Dashlane+ data-bwignore // Bitwarden/><InputGroupAddon align="inline-end"><InputGroupText>factors</InputGroupText>@@ -487,6 +515,10 @@ export const MfaAuthSettingsForm = () => {max={30}{...field}disabled={!canUpdateConfig || !hasAccessToMFA}+ data-1p-ignore // 1Password+ data-lpignore="true" // LastPass+ data-form-type="other" // Dashlane+ data-bwignore // Bitwarden/><InputGroupAddon align="inline-end"><InputGroupText>digits</InputGroupText>@@ -513,6 +545,10 @@ export const MfaAuthSettingsForm = () => {type="text"{...field}disabled={!canUpdateConfig || !hasAccessToMFA}+ data-1p-ignore // 1Password+ data-lpignore="true" // LastPass+ data-form-type="other" // Dashlane+ data-bwignore // Bitwarden/></FormControl_Shadcn_></FormItemLayout>@@ -630,7 +666,7 @@ export const MfaAuthSettingsForm = () => {disabled={!canUpdateConfig || isUpdatingSecurityForm || !securityForm.formState.isDirty}- loading={isUpdatingPhoneForm}+ loading={isUpdatingSecurityForm}>Save changes</Button>