Files
supabase/apps/studio/data/recovery-codes/recovery-codes-generate-mutation.ts
Gildas Garcia 24f89f3967 MFA Recovery codes: allow users to regenerate their codes (#50336)
## 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 -->
2026-09-15 16:38:41 +02:00

51 lines
1.5 KiB
TypeScript

import type {
AuthMFARecoveryCodesGenerateResponse,
MFARecoveryCodesGenerateParams,
} from '@supabase/auth-js'
import { useMutation } from '@tanstack/react-query'
import { toast } from 'sonner'
import { captureCriticalError } from '@/lib/error-reporting'
import { auth } from '@/lib/gotrue'
import { UseCustomMutationOptions } from '@/types'
export const recoveryCodesGenerate = async (params: MFARecoveryCodesGenerateParams) => {
const { error, data } = await auth.mfa.recoveryCodes.generate(params)
if (error) throw error
return data
}
type RecoveryCodesGenerateResponse = NonNullable<AuthMFARecoveryCodesGenerateResponse['data']>
type RecoveryCodesGenerateError = NonNullable<AuthMFARecoveryCodesGenerateResponse['error']>
export const useRecoveryCodesGenerateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<
RecoveryCodesGenerateResponse,
RecoveryCodesGenerateError,
MFARecoveryCodesGenerateParams
>,
'mutationFn'
> = {}) => {
return useMutation({
mutationFn: (vars) => {
return recoveryCodesGenerate(vars)
},
async onSuccess(data, variables, context) {
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to generate recovery codes: ${data.message}`)
} else {
onError(data, variables, context)
}
captureCriticalError(data, 'recovery codes status')
},
...options,
})
}