mirror of
https://github.com/supabase/supabase.git
synced 2026-10-15 06:09:08 +03:00
* feat: add mfa enforcement toggle Allows toggling MFA enforcement on an organization level. Depends on API change #22088 * fix: toggle and disable action for non-owner * chore: disable for free plans * chore: disable toggle for org MFA if current user doesn't have MFA enabled * set initial state * move org mfa to security page * use zod resolver * fix initial values * useQueryClient in the organization-mfa-mutation * chore: warn about losing access to mfa orgs * Some refactors, fixes and improvements * chore: feature flag mfa enforce page * Tiny fix * Leave comment --------- Co-authored-by: Saxon Fletcher <saxonafletcher@gmail.com> Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
58 lines
1.7 KiB
TypeScript
58 lines
1.7 KiB
TypeScript
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { patch, handleError } from 'data/fetchers'
|
|
import type { ResponseError } from 'types'
|
|
import { organizationKeys } from './keys'
|
|
|
|
export type OrganizationMfaToggleVariables = {
|
|
slug: string
|
|
setEnforced: boolean
|
|
}
|
|
|
|
export async function toggleOrganizationMfa({ slug, setEnforced }: OrganizationMfaToggleVariables) {
|
|
if (!slug) {
|
|
throw new Error('Slug is required')
|
|
}
|
|
const { data, error } = await patch('/platform/organizations/{slug}/members/mfa/enforcement', {
|
|
params: { path: { slug } },
|
|
body: { enforced: setEnforced },
|
|
})
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type OrganizationMfaToggleData = Awaited<ReturnType<typeof toggleOrganizationMfa>>
|
|
|
|
export const useOrganizationMfaToggleMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<OrganizationMfaToggleData, ResponseError, OrganizationMfaToggleVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<OrganizationMfaToggleData, ResponseError, OrganizationMfaToggleVariables>(
|
|
(vars) => toggleOrganizationMfa(vars),
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
const { slug } = variables
|
|
|
|
// We already have the data, no need to refetch
|
|
queryClient.setQueryData(organizationKeys.mfa(slug), data.enforced)
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update MFA enforcement: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|