mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 01:45:10 +03:00
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import type { AuthMFAEnrollResponse, MFAEnrollParams } from '@supabase/supabase-js'
|
|
import { useMutation } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { auth } from '@/lib/gotrue'
|
|
import { UseCustomMutationOptions } from '@/types'
|
|
|
|
const mfaEnroll = async (params: MFAEnrollParams) => {
|
|
const { error, data } = await auth.mfa.enroll(params)
|
|
|
|
if (error) throw error
|
|
return data
|
|
}
|
|
|
|
type CustomMFAEnrollResponse = NonNullable<AuthMFAEnrollResponse['data']>
|
|
type CustomMFAEnrollError = NonNullable<AuthMFAEnrollResponse['error']>
|
|
|
|
export const useMfaEnrollMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<CustomMFAEnrollResponse, CustomMFAEnrollError, MFAEnrollParams>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
return useMutation({
|
|
mutationFn: (vars) => mfaEnroll(vars),
|
|
async onSuccess(data, variables, context) {
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to enroll factor: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|