Files
Joshen Lim 7c52a9b539 Joshenlim/fe 3931 re phone mfa enrollment repeatedly force disabled despite (#52629)
## Context

In Authentication -> Multi Factor, if you've set SMS MFA to any status
apart from "Disabled" and refresh the page, the UI seemingly doesn't
render the right value in the dropdown as such:
<img width="1114" height="160" alt="image"
src="https://github.com/user-attachments/assets/d3353628-0b9a-4347-b81f-3a968a87b5c2"
/>

Was happening as the form mounted with placeholder values (for SMS MFA,
default status was "Disabled"), and the real value was only applied
afterwards by a `useEffect` which called `reset`. This changed the
`Select`'s value after mount, in Radix then fires an event on that
change through `onValueChange` which set `value` as an empty string
`''`. It happens as well for TOTP, if you set any value other than
"Enabled" which is the default status.

## Changes involved
- Am also opting to breakdown `MfaAuthSettingsForm.tsx` into smaller
components as it's a single file handling 3 different forms at the
moment
- (Main fix) Derive the form values via `defaultValue` instead of via
`useEffect` which prevents the unintended firing of `onValueChange` for
the `Select`
2026-11-03 14:38:39 +08:00

101 lines
3.3 KiB
TypeScript

import { useParams } from 'common'
import { PageSection, PageSectionContent } from 'ui-patterns/PageSection'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
import { EnhancedMfaSecurityForm } from './EnhancedMfaSecurityForm'
import { PhoneMfaForm } from './PhoneMfaForm'
import { TotpMfaForm } from './TotpMfaForm'
import { AlertError } from '@/components/ui/AlertError'
import { NoPermission } from '@/components/ui/NoPermission'
import { useAuthConfigQuery } from '@/data/auth/auth-config-query'
import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { IS_PLATFORM } from '@/lib/constants'
export const MfaAuthSettingsForm = () => {
const { ref: projectRef } = useParams()
const {
data: authConfig,
error: authConfigError,
isError,
isPending: isLoading,
} = useAuthConfigQuery({ projectRef })
const { data: project, isPending: isLoadingProject } = useSelectedProjectQuery()
const isBranch = project?.parentRef !== project?.ref
const { can: canReadConfig } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.PROJECT.AUTH_CONFIG_READ
)
const { can: canUpdateConfig } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.PROJECT.AUTH_CONFIG_WRITE
)
const { hasAccess: hasAccessToMFAEntitlement, isLoading: isLoadingEntitlement } =
useCheckEntitlements('auth.mfa_phone')
const hasAccessToMFA = !IS_PLATFORM || hasAccessToMFAEntitlement
const promptProPlanUpgrade = IS_PLATFORM && !hasAccessToMFAEntitlement
const {
hasAccess: hasAccessToEnhanceSecurityEntitlement,
isLoading: isLoadingEntitlementEnhanceSecurity,
} = useCheckEntitlements('auth.mfa_enhanced_security')
const hasAccessToEnhanceSecurity = !IS_PLATFORM || hasAccessToEnhanceSecurityEntitlement
const promptEnhancedSecurityUpgrade = IS_PLATFORM && !hasAccessToEnhanceSecurityEntitlement
if (isError) {
return (
<PageSection>
<PageSectionContent>
<AlertError error={authConfigError} subject="Failed to retrieve auth configuration" />
</PageSectionContent>
</PageSection>
)
}
if (!canReadConfig) {
return (
<PageSection>
<PageSectionContent>
<NoPermission resourceText="view auth configuration settings" />
</PageSectionContent>
</PageSection>
)
}
if (
isLoading ||
isLoadingProject ||
isLoadingEntitlement ||
isLoadingEntitlementEnhanceSecurity ||
!authConfig
) {
return (
<PageSection>
<PageSectionContent>
<GenericSkeletonLoader />
</PageSectionContent>
</PageSection>
)
}
return (
<>
<TotpMfaForm authConfig={authConfig} canUpdateConfig={canUpdateConfig} />
<PhoneMfaForm
authConfig={authConfig}
canUpdateConfig={canUpdateConfig}
hasAccessToMFA={hasAccessToMFA}
promptProPlanUpgrade={promptProPlanUpgrade}
isBranch={isBranch}
/>
<EnhancedMfaSecurityForm
authConfig={authConfig}
canUpdateConfig={canUpdateConfig}
hasAccessToEnhanceSecurity={hasAccessToEnhanceSecurity}
promptEnhancedSecurityUpgrade={promptEnhancedSecurityUpgrade}
/>
</>
)
}