mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## What kind of change does this PR introduce? Once users have recovery codes generated, allow them to regenerate the codes. This PR also automatically check the _I have copied the codes_ after clicking the _Copy to clipboard button_. > [!NOTE] > The _Delete my recovery codes_ button only appear on local and staging environments ## How to test - On an account that already have recovery codes generated - You should see an admonition showing the remaining codes available and allowing you to regenerate the codes ## Screenshots <img width="706" height="193" alt="image" src="https://github.com/user-attachments/assets/001bfa87-74f5-4867-8564-09cb6f91adb6" /> <img width="425" height="277" alt="image" src="https://github.com/user-attachments/assets/711b139c-f806-4da2-a240-fa7e7fd8acd0" /> <img width="548" height="353" alt="image" src="https://github.com/user-attachments/assets/d6521a09-3a7f-4198-b162-9effc218fee6" /> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added a recovery-code modal with copy-to-clipboard support and confirmation before closing. - Added an option to regenerate MFA recovery codes with a confirmation step. - Recovery-code controls now appear when existing codes are available. - Added loading, success, error, and retry states for recovery-code generation and regeneration. - **Bug Fixes** - Updated the recovery-code generation error message to more accurately describe the failed action. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
import type { AuthMFARecoveryCodesGenerateResponse } from '@supabase/auth-js'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { recoveryCodeKeys } from './keys'
|
|
import { captureCriticalError } from '@/lib/error-reporting'
|
|
import { auth } from '@/lib/gotrue'
|
|
import { UseCustomMutationOptions } from '@/types'
|
|
|
|
export const recoveryCodesRegenerate = async () => {
|
|
const { error, data } = await auth.mfa.recoveryCodes.regenerate()
|
|
if (error) throw error
|
|
return data
|
|
}
|
|
|
|
type RecoveryCodesRegenerateResponse = NonNullable<AuthMFARecoveryCodesGenerateResponse['data']>
|
|
type RecoveryCodesRegenerateError = NonNullable<AuthMFARecoveryCodesGenerateResponse['error']>
|
|
|
|
export const useRecoveryCodesRegenerateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<RecoveryCodesRegenerateResponse, RecoveryCodesRegenerateError>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: () => {
|
|
return recoveryCodesRegenerate()
|
|
},
|
|
async onSuccess(data, variables, context) {
|
|
await queryClient.invalidateQueries({ queryKey: recoveryCodeKeys.status() })
|
|
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to regenerate recovery codes: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
captureCriticalError(data, 'recovery codes regenerate')
|
|
},
|
|
...options,
|
|
})
|
|
}
|