mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## Problem Studio tracks completion of recovery setup, but there are no events for copying or downloading the codes. ## Solution Track both actions in the shared recovery-code dialog. Record a copy after the clipboard write succeeds and a download when the browser starts it. The events carry no recovery-code values. ## Review instructions 1. Open [Recovery codes](https://supabase.com/dashboard/account/security#recovery-codes) on this branch's Studio preview with a disposable MFA account. Select **Generate recovery codes** or **Regenerate** if it already has a set. 2. In **Save your recovery codes**, select **Copy**, then **Download**. Check the browser network panel for `recovery_codes_copied` and `recovery_codes_download_clicked` in the telemetry requests (`telemetry/event`).
233 lines
8.0 KiB
TypeScript
233 lines
8.0 KiB
TypeScript
import { AuthError, AuthMFARecoveryCodesGenerateResponseData } from '@supabase/auth-js'
|
|
import { UseMutationResult, useQueryClient } from '@tanstack/react-query'
|
|
import type { RecoveryCodesSurface } from 'common/telemetry-constants'
|
|
import { CheckIcon, CopyIcon, DownloadIcon } from 'lucide-react'
|
|
import { ComponentProps, useEffect, useMemo, useState } from 'react'
|
|
import {
|
|
Button,
|
|
Checkbox,
|
|
cn,
|
|
copyToClipboard,
|
|
Dialog,
|
|
DialogClose,
|
|
DialogContent,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogSection,
|
|
DialogSectionSeparator,
|
|
DialogTitle,
|
|
} from 'ui'
|
|
import { Admonition } from 'ui-patterns/Admonition'
|
|
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
|
|
|
|
import { formatRecoveryCode } from './RecoveryCodesModal.utils'
|
|
import { AlertError } from '@/components/ui/AlertError'
|
|
import { InlineLink } from '@/components/ui/InlineLink'
|
|
import { recoveryCodeKeys } from '@/data/recovery-codes/keys'
|
|
import { useTrack } from '@/lib/telemetry/track'
|
|
|
|
interface RecoveryCodesModalProps<T>
|
|
extends
|
|
Omit<ComponentProps<typeof Dialog>, 'onOpenChange'>,
|
|
Required<Pick<ComponentProps<typeof Dialog>, 'onOpenChange'>> {
|
|
surface: RecoveryCodesSurface
|
|
showSecurityLink?: boolean
|
|
mutation: Pick<
|
|
UseMutationResult<AuthMFARecoveryCodesGenerateResponseData, AuthError, T>,
|
|
'data' | 'status' | 'error' | 'isError' | 'isPending' | 'isSuccess' | 'reset'
|
|
>
|
|
}
|
|
|
|
export const RecoveryCodesModal = <T = unknown,>({
|
|
onOpenChange,
|
|
mutation,
|
|
surface,
|
|
showSecurityLink = false,
|
|
...props
|
|
}: RecoveryCodesModalProps<T>) => {
|
|
const queryClient = useQueryClient()
|
|
const track = useTrack()
|
|
const [copied, setCopied] = useState(false)
|
|
const [copiedToClipboard, setCopiedToClipboard] = useState(false)
|
|
|
|
const { data, status, error, isError, isPending, isSuccess } = mutation
|
|
const codes = data?.codes ?? []
|
|
|
|
const title = useMemo(() => {
|
|
switch (status) {
|
|
case 'pending':
|
|
return 'Generating your recovery codes...'
|
|
case 'error':
|
|
return 'An error occurred while generating your recovery code'
|
|
default:
|
|
return 'Save your recovery codes'
|
|
}
|
|
}, [status])
|
|
|
|
const downloadCodes = () => {
|
|
const blob = new Blob([codes.map((code) => formatRecoveryCode(code)).join('\n')], {
|
|
type: 'text/plain;charset=utf-8;',
|
|
})
|
|
const url = window.URL.createObjectURL(blob)
|
|
const a = document.createElement('a')
|
|
a.href = url
|
|
a.setAttribute('download', 'supabase-recovery-codes.txt')
|
|
document.body.appendChild(a)
|
|
a.click()
|
|
document.body.removeChild(a)
|
|
window.URL.revokeObjectURL(url)
|
|
track('recovery_codes_download_clicked', { surface })
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (copiedToClipboard) {
|
|
setTimeout(() => setCopiedToClipboard(false), 4000)
|
|
}
|
|
}, [copiedToClipboard])
|
|
|
|
return (
|
|
<Dialog
|
|
{...props}
|
|
onOpenChange={(open) => {
|
|
// Prevent users from closing the dialog until they copied the codes
|
|
if (!open && !copied && mutation.isSuccess) return
|
|
// Prevent users from closing the dialog while the mutation is running
|
|
if (mutation.isPending) return
|
|
|
|
onOpenChange(open)
|
|
if (!open) {
|
|
setCopied(false)
|
|
setCopiedToClipboard(false)
|
|
mutation.reset()
|
|
queryClient.invalidateQueries({ queryKey: recoveryCodeKeys.status() })
|
|
}
|
|
}}
|
|
>
|
|
<DialogContent hideClose aria-describedby={undefined}>
|
|
<DialogHeader>
|
|
<DialogTitle aria-busy={mutation.isPending} aria-live="polite" role="status">
|
|
{title}
|
|
</DialogTitle>
|
|
</DialogHeader>
|
|
|
|
<DialogSectionSeparator />
|
|
|
|
<DialogSection className={cn(isError && 'p-0 md:px-0')}>
|
|
{isPending && (
|
|
<div role="status" aria-label="Generating recovery codes" className="space-y-3">
|
|
<ShimmeringLoader className="h-4 py-0" />
|
|
<ShimmeringLoader className="h-4 w-3/4 py-0" />
|
|
<ShimmeringLoader className="h-32 py-0" />
|
|
</div>
|
|
)}
|
|
{isError && (
|
|
<AlertError
|
|
layout="vertical"
|
|
className="rounded-none border-0"
|
|
subject="Unable to generate recovery codes"
|
|
error={error}
|
|
/>
|
|
)}
|
|
|
|
{isSuccess && (
|
|
<div className="text-sm flex flex-col gap-4">
|
|
<p>
|
|
Recovery codes allow you to recover your account in case you lost access to your MFA
|
|
apps. Save them somewhere safe.
|
|
</p>
|
|
|
|
<div className="flex flex-col gap-y-2">
|
|
<ul className="bg-muted rounded-md p-4 border grid grid-cols-2 gap-2">
|
|
{codes?.map((code, idx) => (
|
|
<li key={code} className="font-mono text-sm flex gap-x-3">
|
|
<span className="text-foreground-lighter w-4 inline-block text-right select-none">
|
|
{idx + 1}
|
|
</span>
|
|
<span>{formatRecoveryCode(code)}</span>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
|
|
<div className="flex items-center gap-x-2">
|
|
<Button
|
|
icon={
|
|
copiedToClipboard ? (
|
|
<CheckIcon className="text-brand-default" />
|
|
) : (
|
|
<CopyIcon />
|
|
)
|
|
}
|
|
className="ml-auto w-min"
|
|
onClick={() =>
|
|
copyToClipboard(
|
|
codes.map((code) => formatRecoveryCode(code)).join('\n') ?? '',
|
|
() => {
|
|
setCopiedToClipboard(true)
|
|
track('recovery_codes_copied', { surface })
|
|
}
|
|
)
|
|
}
|
|
>
|
|
{copiedToClipboard ? 'Copied' : 'Copy'}
|
|
</Button>
|
|
<Button icon={<DownloadIcon />} onClick={downloadCodes}>
|
|
Download
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
<span className="sr-only" aria-live="polite">
|
|
{copiedToClipboard && 'Codes copied to clipboard'}
|
|
</span>
|
|
|
|
<Admonition
|
|
type="warning"
|
|
title="You won't see these codes again"
|
|
description="Save them somewhere safe. If you lose them, you'll need to regenerate new codes."
|
|
>
|
|
<div className="flex items-center space-x-2 mt-2">
|
|
<Checkbox
|
|
id="codeCopied"
|
|
checked={copied}
|
|
onCheckedChange={(checked) => setCopied(checked === true)}
|
|
/>
|
|
<label
|
|
htmlFor="codeCopied"
|
|
className="text-sm text-warning leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
|
|
>
|
|
I have saved my recovery codes somewhere safe
|
|
</label>
|
|
</div>
|
|
</Admonition>
|
|
{showSecurityLink && (
|
|
<p className="text-xs text-foreground-lighter">
|
|
Manage recovery codes in{' '}
|
|
<InlineLink href="/account/security" target="_blank" rel="noreferrer">
|
|
Account Security
|
|
<span className="sr-only"> (opens in a new tab)</span>
|
|
</InlineLink>
|
|
.
|
|
</p>
|
|
)}
|
|
</div>
|
|
)}
|
|
</DialogSection>
|
|
|
|
{!isPending && (
|
|
<DialogFooter className="items-center">
|
|
<DialogClose
|
|
asChild
|
|
disabled={!copied && !isError}
|
|
className={copied || isError ? 'opacity-100' : ''}
|
|
>
|
|
<Button variant={isSuccess ? 'primary' : 'default'}>
|
|
{isSuccess ? 'Done' : 'Close'}
|
|
</Button>
|
|
</DialogClose>
|
|
</DialogFooter>
|
|
)}
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|