Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Joshenlim/fe 4522 polish recovery codes UI (#51124)

Joshen Limacaf640d1c61
diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/AddNewFactorModal.tsx b/apps/studio/components/interfaces/Account/TOTPFactors/AddNewFactorModal.tsx
index bbc79658cae..d55983a974a 100644
--- a/apps/studio/components/interfaces/Account/TOTPFactors/AddNewFactorModal.tsx
+++ b/apps/studio/components/interfaces/Account/TOTPFactors/AddNewFactorModal.tsx
@@ -246,7 +246,7 @@ const SecondStep = ({
complete the enrolment.
</p>
- {isLoading && (
+ {!factor && (
<div className="pb-4 px-4">
<GenericSkeletonLoader />
</div>
diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/GenerateRecoveryCodesModal.test.tsx b/apps/studio/components/interfaces/Account/TOTPFactors/GenerateRecoveryCodesModal.test.tsx
index 562ca916e2a..bc0c051ec70 100644
--- a/apps/studio/components/interfaces/Account/TOTPFactors/GenerateRecoveryCodesModal.test.tsx
+++ b/apps/studio/components/interfaces/Account/TOTPFactors/GenerateRecoveryCodesModal.test.tsx
@@ -49,21 +49,21 @@ describe('GenerateRecoveryCodesModal', () => {
await screen.findByText('WTO2-4T5X-BEUL-VJMI')
await screen.findByText('KYYF-SP3E-QJYD-J53T')
- // Users have to copy the codes to close the modal, next click should fail if they managed to close it
- expect(await screen.findAllByRole('button', { name: 'Close' })).toHaveLength(1)
- fireEvent.click(await screen.findByRole('button', { name: 'Close' }))
- fireEvent.click(await screen.findByRole('button', { name: 'Copy to clipboard' }))
- await waitFor(() =>
- expect(screen.getByRole('checkbox', { name: 'I have copied the codes' })).toBeChecked()
+ // Users have to copy the codes before they can close the modal
+ expect(await screen.findByRole('button', { name: 'Done' })).toBeDisabled()
+ fireEvent.click(await screen.findByRole('button', { name: 'Copy' }))
+ fireEvent.click(
+ screen.getByRole('checkbox', { name: 'I have saved my recovery codes somewhere safe' })
)
expect(mockCopyToClipboard).toHaveBeenCalledWith(
codes.map((code) => formatRecoveryCode(code)).join('\n'),
expect.any(Function)
)
- // We should have 2 close buttons (header icon and a standard button)
- expect(await screen.findAllByRole('button', { name: 'Close' })).toHaveLength(2)
- fireEvent.click((await screen.findAllByRole('button', { name: 'Close' })).at(1)!)
+ // Done button is now enabled and closes the modal
+ const doneButton = await screen.findByRole('button', { name: 'Done' })
+ expect(doneButton).toBeEnabled()
+ fireEvent.click(doneButton)
await waitFor(() => expect(screen.queryByText('Save your recovery codes')).toBeNull())
})
@@ -84,13 +84,12 @@ describe('GenerateRecoveryCodesModal', () => {
})
customRender(<GenerateRecoveryCodesModal />)
fireEvent.click(await screen.findByRole('button', { name: 'Generate recovery codes' }))
+ await screen.findByText('Unable to generate recovery codes')
await screen.findByText(
- "We couldn't generate your recovery code. Please try again later or contact support if the problem persists."
+ 'Try refreshing your browser, but if the issue persists for more than a few minutes, please reach out to us via support.'
)
- // We should have 2 close buttons (header icon and a standard button)
- expect(await screen.findAllByRole('button', { name: 'Close' })).toHaveLength(2)
- fireEvent.click((await screen.findAllByRole('button', { name: 'Close' })).at(1)!)
+ fireEvent.click(await screen.findByRole('button', { name: 'Close' }))
await waitFor(() => expect(screen.queryByText('Save your recovery codes')).toBeNull())
// Retry
diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/GenerateRecoveryCodesModal.tsx b/apps/studio/components/interfaces/Account/TOTPFactors/GenerateRecoveryCodesModal.tsx
index f80a8683186..a27b03d07c5 100644
--- a/apps/studio/components/interfaces/Account/TOTPFactors/GenerateRecoveryCodesModal.tsx
+++ b/apps/studio/components/interfaces/Account/TOTPFactors/GenerateRecoveryCodesModal.tsx
@@ -9,26 +9,20 @@ export const GenerateRecoveryCodesModal = () => {
const recoveryCodesGenerateMutation = useRecoveryCodesGenerateMutation({
onSettled: () => setOpen(true),
})
+ const { mutate, isPending } = recoveryCodesGenerateMutation
+
const [open, setOpen] = useState(false)
return (
<Admonition
- type="danger"
+ type="warning"
layout="horizontal"
- title="You haven't generated recovery codes yet"
- description="Recovery codes are important to ensure you can recover your account if you loose access to your MFA."
+ title="No recovery codes generated"
+ description="Recovery codes let you access your account if you lose access to your MFA device"
actions={
<>
- <span aria-live="polite" className="sr-only">
- {recoveryCodesGenerateMutation.isPending ? 'Generating your recovery codes...' : ''}
- </span>
- <Button
- onClick={() => {
- recoveryCodesGenerateMutation.mutate({})
- }}
- loading={recoveryCodesGenerateMutation.isPending}
- >
- Generate recovery codes
+ <Button onClick={() => mutate({})} loading={isPending}>
+ {isPending ? 'Generating codes' : 'Generate recovery codes'}
</Button>
<RecoveryCodesModal
open={open}
diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.tsx b/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.tsx
index c16cb52bfa3..a65daf29ed9 100644
--- a/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.tsx
+++ b/apps/studio/components/interfaces/Account/TOTPFactors/RecoveryCodesModal.tsx
@@ -1,20 +1,25 @@
import { AuthError, AuthMFARecoveryCodesGenerateResponseData } from '@supabase/auth-js'
-import { MutationStatus, UseMutationResult, useQueryClient } from '@tanstack/react-query'
-import { ComponentProps, useState } from 'react'
+import { UseMutationResult, useQueryClient } from '@tanstack/react-query'
+import { Check, Copy, Download } from 'lucide-react'
+import { ComponentProps, useEffect, useMemo, useState } from 'react'
import {
Button,
Checkbox,
+ cn,
copyToClipboard,
Dialog,
DialogClose,
DialogContent,
- DialogDescription,
DialogFooter,
DialogHeader,
+ DialogSection,
+ DialogSectionSeparator,
DialogTitle,
} from 'ui'
+import { Admonition } from 'ui-patterns/Admonition'
import { formatRecoveryCode } from './RecoveryCodesModal.utils'
+import { AlertError } from '@/components/ui/AlertError'
import { recoveryCodeKeys } from '@/data/recovery-codes/keys'
interface RecoveryCodesModalProps<T>
@@ -33,6 +38,40 @@ export const RecoveryCodesModal = <T = unknown,>({
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)
+ }
+
+ useEffect(() => {
+ if (copiedToClipboard) {
+ setTimeout(() => setCopiedToClipboard(false), 4000)
+ }
+ }, [copiedToClipboard])
+
return (
<Dialog
{...props}
@@ -44,7 +83,6 @@ export const RecoveryCodesModal = <T = unknown,>({
onOpenChange(open)
if (!open) {
- // Reset state
setCopied(false)
setCopiedToClipboard(false)
mutation.reset()
@@ -52,117 +90,104 @@ export const RecoveryCodesModal = <T = unknown,>({
}
}}
>
- <DialogContent>
+ <DialogContent hideClose aria-describedby={undefined}>
<DialogHeader>
<DialogTitle aria-busy={mutation.isPending} aria-live="polite" role="status">
- <GenerateRecoveryCodesModalTitle status={mutation.status} />
+ {title}
</DialogTitle>
- <DialogDescription asChild>
- <div className="py-4">
- <GenerateRecoveryCodesModalContent
- status={mutation.status}
- codes={mutation.data?.codes}
- copied={copied}
- onCodesCopied={(copied) => setCopied(copied)}
- />
- </div>
- </DialogDescription>
</DialogHeader>
- {!mutation.isPending ? (
- <DialogFooter className="items-center">
- <span role="status" className="text-sm text-lighter">
- {copiedToClipboard ? 'Codes copied to your clipboard.' : null}
- </span>
- {copied || mutation.isError ? (
- <DialogClose asChild>
- <Button>Close</Button>
- </DialogClose>
- ) : null}
-
- {mutation.isSuccess ? (
- <Button
- variant="primary"
- onClick={() =>
- copyToClipboard(
- mutation.data?.codes.map((code) => formatRecoveryCode(code)).join('\n') ?? '',
- () => {
- setCopiedToClipboard(true)
- setCopied(true)
+
+ <DialogSectionSeparator />
+
+ <DialogSection className={cn(isError && 'p-0 md:px-0')}>
+ {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 ? <Check className="text-brand-default" /> : <Copy />}
+ className="ml-auto w-min"
+ onClick={() =>
+ copyToClipboard(
+ codes.map((code) => formatRecoveryCode(code)).join('\n') ?? '',
+ () => setCopiedToClipboard(true)
+ )
}
- )
- }
+ >
+ {copiedToClipboard ? 'Copied' : 'Copy'}
+ </Button>
+ <Button icon={<Download />} 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."
>
- Copy to clipboard
+ <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>
+ </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>
- ) : null}
+ </DialogClose>
</DialogFooter>
- ) : null}
+ )}
</DialogContent>
</Dialog>
)
}
-
-const GenerateRecoveryCodesModalTitle = ({ status }: { status: MutationStatus }) => {
- if (status === 'pending') {
- return 'Generating your recovery codes...'
- }
- if (status === 'error') {
- return 'An error occurred while generating your recovery code'
- }
-
- return 'Save your recovery codes'
-}
-
-const GenerateRecoveryCodesModalContent = ({
- codes,
- copied,
- status,
- onCodesCopied,
-}: {
- codes: AuthMFARecoveryCodesGenerateResponseData['codes'] | undefined
- copied?: boolean
- status: MutationStatus
- onCodesCopied: (copied: boolean) => void
-}) => {
- if (status === 'error') {
- return (
- <p className="text-destructive">
- We couldn't generate your recovery code. Please try again later or contact support if the
- problem persists.
- </p>
- )
- }
-
- if (status === 'success') {
- return (
- <div className="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>
- <pre className="relative bg-muted rounded-md py-2 px-4">
- <code className="flex gap-2 flex-wrap justify-between">
- {codes?.map((code) => (
- <span key={code}>{formatRecoveryCode(code)}</span>
- ))}
- </code>
- </pre>
- <div className="flex items-center space-x-2">
- <Checkbox
- id="codeCopied"
- checked={copied}
- onCheckedChange={(checked) => onCodesCopied(checked === true)}
- />
- <label
- htmlFor="codeCopied"
- className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
- >
- I have copied the codes
- </label>
- </div>
- </div>
- )
- }
-
- return null
-}
diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.test.tsx b/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.test.tsx
index 85836832e08..375ac080355 100644
--- a/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.test.tsx
+++ b/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.test.tsx
@@ -43,7 +43,7 @@ describe('RegenerateRecoveryCodesModal', () => {
error: null,
})
customRender(<RegenerateRecoveryCodesModal />)
- fireEvent.click(await screen.findByRole('button', { name: 'Regenerate my recovery codes' }))
+ fireEvent.click(await screen.findByRole('button', { name: 'Regenerate' }))
// Confirm regeneration
await userEvent.type(
@@ -61,21 +61,22 @@ describe('RegenerateRecoveryCodesModal', () => {
await screen.findByText('WTO2-4T5X-BEUL-VJMI')
await screen.findByText('KYYF-SP3E-QJYD-J53T')
- // Users have to copy the codes to close the modal, next click should fail if they managed to close it
- expect(await screen.findAllByRole('button', { name: 'Close' })).toHaveLength(1)
- fireEvent.click(await screen.findByRole('button', { name: 'Close' }))
- fireEvent.click(await screen.findByRole('button', { name: 'Copy to clipboard' }))
- await waitFor(() =>
- expect(screen.getByRole('checkbox', { name: 'I have copied the codes' })).toBeChecked()
+ // Users have to copy the codes before they can close the modal
+ expect(await screen.findByRole('button', { name: 'Done' })).toBeDisabled()
+ fireEvent.click(await screen.findByRole('button', { name: 'Copy' }))
+ fireEvent.click(
+ screen.getByRole('checkbox', { name: 'I have saved my recovery codes somewhere safe' })
)
+
expect(mockCopyToClipboard).toHaveBeenCalledWith(
codes.map((code) => formatRecoveryCode(code)).join('\n'),
expect.any(Function)
)
- // We should have 2 close buttons (header icon and a standard button)
- expect(await screen.findAllByRole('button', { name: 'Close' })).toHaveLength(2)
- fireEvent.click((await screen.findAllByRole('button', { name: 'Close' })).at(1)!)
+ // Done button is now enabled and closes the modal
+ const doneButton = await screen.findByRole('button', { name: 'Done' })
+ expect(doneButton).toBeEnabled()
+ fireEvent.click(doneButton)
await waitFor(() => expect(screen.queryByText('Save your recovery codes')).toBeNull())
})
@@ -95,7 +96,7 @@ describe('RegenerateRecoveryCodesModal', () => {
error: null,
})
customRender(<RegenerateRecoveryCodesModal />)
- fireEvent.click(await screen.findByRole('button', { name: 'Regenerate my recovery codes' }))
+ fireEvent.click(await screen.findByRole('button', { name: 'Regenerate' }))
// Confirm regeneration
await userEvent.type(
@@ -107,17 +108,16 @@ describe('RegenerateRecoveryCodesModal', () => {
fireEvent.click(
await within(await screen.findByRole('dialog')).findByRole('button', { name: 'Regenerate' })
)
+ await screen.findByText('Unable to generate recovery codes')
await screen.findByText(
- "We couldn't generate your recovery code. Please try again later or contact support if the problem persists."
+ 'Try refreshing your browser, but if the issue persists for more than a few minutes, please reach out to us via support.'
)
- // We should have 2 close buttons (header icon and a standard button)
- expect(await screen.findAllByRole('button', { name: 'Close' })).toHaveLength(2)
- fireEvent.click((await screen.findAllByRole('button', { name: 'Close' })).at(1)!)
+ fireEvent.click(await screen.findByRole('button', { name: 'Close' }))
await waitFor(() => expect(screen.queryByText('Save your recovery codes')).toBeNull())
// Retry
- fireEvent.click(await screen.findByRole('button', { name: 'Regenerate my recovery codes' }))
+ fireEvent.click(await screen.findByRole('button', { name: 'Regenerate' }))
// Confirm regeneration
await userEvent.type(
await within(await screen.findByRole('dialog')).findByRole('textbox', {
diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.tsx b/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.tsx
index 68d034c204e..d1a9c372249 100644
--- a/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.tsx
+++ b/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.tsx
@@ -1,3 +1,4 @@
+import { RefreshCw } from 'lucide-react'
import { useState } from 'react'
import { Button } from 'ui'
@@ -16,25 +17,28 @@ export const RegenerateRecoveryCodesModal = () => {
},
})
+ const { mutate: regenerateCodes, isPending } = recoveryCodesRegenerateMutation
+
return (
<>
- <Button onClick={() => setShowConfirm(true)}>Regenerate my recovery codes</Button>
+ <Button icon={<RefreshCw />} onClick={() => setShowConfirm(true)}>
+ Regenerate
+ </Button>
<TextConfirmModal
visible={showConfirm}
size="small"
- variant="destructive"
+ variant="warning"
title="Regenerate my recovery codes"
confirmPlaceholder="REGENERATE"
confirmString="REGENERATE"
confirmLabel="Regenerate"
- loading={recoveryCodesRegenerateMutation.isPending}
- onConfirm={() => {
- recoveryCodesRegenerateMutation.mutate()
- }}
+ loading={isPending}
+ onConfirm={() => regenerateCodes()}
onCancel={() => setShowConfirm(false)}
>
<p className="text-sm">Your existing recovery codes won't work anymore.</p>
</TextConfirmModal>
+
<RecoveryCodesModal
open={open}
onOpenChange={(open) => setOpen(open)}
diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/UnenrollRecoveryCodesModal.tsx b/apps/studio/components/interfaces/Account/TOTPFactors/UnenrollRecoveryCodesModal.tsx
index a86f4e914bc..7ea00cee694 100644
--- a/apps/studio/components/interfaces/Account/TOTPFactors/UnenrollRecoveryCodesModal.tsx
+++ b/apps/studio/components/interfaces/Account/TOTPFactors/UnenrollRecoveryCodesModal.tsx
@@ -1,20 +1,29 @@
+import { Trash } from 'lucide-react'
import { useState } from 'react'
-import { Button } from 'ui'
+import { toast } from 'sonner'
+import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { TextConfirmModal } from '@/components/ui/TextConfirmModalWrapper'
import { useRecoveryCodesUnenrollMutation } from '@/data/recovery-codes/recovery-codes-unenroll'
export const UnenrollRecoveryCodesModal = () => {
const [showConfirm, setShowConfirm] = useState(false)
- const recoveryCodesGenerateMutation = useRecoveryCodesUnenrollMutation({
+ const { mutate: unenrollCodes, isPending } = useRecoveryCodesUnenrollMutation({
onSuccess: () => {
+ toast.success('Successfully removed recovery codes')
setShowConfirm(false)
},
})
return (
<>
- <Button onClick={() => setShowConfirm(true)}>Delete my recovery codes</Button>
+ <ButtonTooltip
+ icon={<Trash />}
+ aria-label="Delete recovery codes"
+ onClick={() => setShowConfirm(true)}
+ tooltip={{ content: { side: 'bottom', text: 'Delete recovery codes' } }}
+ />
+
<TextConfirmModal
visible={showConfirm}
size="small"
@@ -23,8 +32,8 @@ export const UnenrollRecoveryCodesModal = () => {
confirmPlaceholder="DELETE"
confirmString="DELETE"
confirmLabel="Delete"
- loading={recoveryCodesGenerateMutation.isPending}
- onConfirm={() => recoveryCodesGenerateMutation.mutate()}
+ loading={isPending}
+ onConfirm={() => unenrollCodes()}
onCancel={() => setShowConfirm(false)}
>
<p className="text-sm">
diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/index.tsx b/apps/studio/components/interfaces/Account/TOTPFactors/index.tsx
index ea277494f14..b229ed1d525 100644
--- a/apps/studio/components/interfaces/Account/TOTPFactors/index.tsx
+++ b/apps/studio/components/interfaces/Account/TOTPFactors/index.tsx
@@ -1,10 +1,9 @@
import { useFlag } from 'common'
import dayjs from 'dayjs'
-import { Plus } from 'lucide-react'
+import { Plus, RectangleEllipsis } from 'lucide-react'
import { useState } from 'react'
import { Button, Card, CardContent, cn } from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
-import { ErrorDisplay } from 'ui-patterns/ErrorDisplay/ErrorDisplay'
import {
PageSection,
PageSectionAside,
@@ -15,6 +14,7 @@ import {
PageSectionTitle,
} from 'ui-patterns/PageSection'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
+import { TimestampInfo } from 'ui-patterns/TimestampInfo'
import { AddNewFactorModal } from './AddNewFactorModal'
import { DeleteFactorModal } from './DeleteFactorModal'
@@ -37,61 +37,23 @@ export const TOTPFactors = () => {
const shouldShowLockoutWarning = isSuccess && totpFactors.length === 1
const shouldVerifyRecoveryCodes = enableAuthRecoveryCodes && totpFactors.length > 0
- const recoveryCodesStatusQuery = useRecoveryCodesStatusQuery({
+ const {
+ data: recoveryCodesStatus,
+ error: recoveryCodesError,
+ isError: recoveryCodesIsError,
+ isPending: recoveryCodesIsPending,
+ } = useRecoveryCodesStatusQuery({
enabled: shouldVerifyRecoveryCodes,
})
+ const { data: codes, status } = recoveryCodesStatus ?? {}
const handleAddNewApp = () => setIsAddNewFactorOpen(true)
// If recovery codes are enabled, we can't allow to remove an MFA until we know their status
- const disableDeleteFactor = shouldVerifyRecoveryCodes && recoveryCodesStatusQuery.isPending
+ const disableDeleteFactor = shouldVerifyRecoveryCodes && recoveryCodesIsPending
return (
<>
- {shouldVerifyRecoveryCodes && (
- <PageSection>
- <PageSectionMeta>
- <PageSectionSummary>
- <PageSectionTitle>Recovery codes</PageSectionTitle>
- <PageSectionDescription>
- Recovery codes allow you to recover your account in case you lost access to your MFA
- apps.
- </PageSectionDescription>
- </PageSectionSummary>
- </PageSectionMeta>
- <PageSectionContent aria-live="polite">
- {recoveryCodesStatusQuery.isError && (
- <ErrorDisplay
- title="Failed to load recovery codes"
- errorMessage="An error occurred while loading recovery codes."
- />
- )}
- {recoveryCodesStatusQuery.data?.status === 'unenrolled' && (
- <GenerateRecoveryCodesModal />
- )}
- {recoveryCodesStatusQuery.data?.status === 'available' &&
- recoveryCodesStatusQuery.data?.data && (
- <Card>
- <CardContent className="flex flex-col gap-2">
- <p
- className={cn(
- 'text-sm',
- recoveryCodesStatusQuery.data.data.remaining < 2 ? 'text-warning' : ''
- )}
- >
- {recoveryCodesStatusQuery.data.data.remaining}/
- {recoveryCodesStatusQuery.data.data.total} recovery codes available
- </p>
- <div className="flex gap-2 ml-auto">
- <RegenerateRecoveryCodesModal />
- <UnenrollRecoveryCodesModal />
- </div>
- </CardContent>
- </Card>
- )}
- </PageSectionContent>
- </PageSection>
- )}
<PageSection>
<PageSectionMeta>
<PageSectionSummary>
@@ -112,7 +74,7 @@ export const TOTPFactors = () => {
<PageSectionContent className="flex flex-col gap-4">
{shouldShowLockoutWarning && (
<Admonition
- type="danger"
+ type="warning"
layout="responsive"
title="Avoid being locked out"
description="Add a backup authenticator app now. Losing access to your only app will permanently lock you out of your account."
@@ -146,7 +108,12 @@ export const TOTPFactors = () => {
<div>
<p className="text-sm">{factor.friendly_name ?? 'No name provided'}</p>
<p className="text-sm text-foreground-lighter">
- Added on {dayjs(factor.created_at).format(DATETIME_FORMAT)}
+ Added on{' '}
+ <TimestampInfo
+ className="text-sm"
+ utcTimestamp={factor.created_at}
+ label={dayjs(factor.created_at).format(DATETIME_FORMAT)}
+ />
</p>
</div>
<Button
@@ -164,16 +131,60 @@ export const TOTPFactors = () => {
)}
</PageSectionContent>
</PageSection>
+
+ {shouldVerifyRecoveryCodes && (
+ <PageSection>
+ <PageSectionMeta>
+ <PageSectionSummary>
+ <PageSectionTitle>Recovery codes</PageSectionTitle>
+ <PageSectionDescription>
+ Recovery codes allow you to recover your account in case you lost access to your MFA
+ apps.
+ </PageSectionDescription>
+ </PageSectionSummary>
+ </PageSectionMeta>
+ <PageSectionContent aria-live="polite">
+ {recoveryCodesIsError && (
+ <AlertError subject="Failed to load recovery codes" error={recoveryCodesError} />
+ )}
+ {status === 'unenrolled' && <GenerateRecoveryCodesModal />}
+ {status === 'available' && codes && (
+ <Card>
+ <CardContent className="flex items-center justify-between">
+ <div className="flex items-center gap-x-4">
+ <div className="w-9 h-9 rounded-full bg-selection flex items-center justify-center">
+ <RectangleEllipsis size={18} className="text-foreground-lighter" />
+ </div>
+ <div className="text-sm">
+ <p className={cn(codes.remaining < 2 ? 'text-warning' : '')}>
+ {codes.remaining}/{codes.total} recovery codes available
+ </p>
+ <p className="text-foreground-lighter">Each code can only be used once</p>
+ </div>
+ </div>
+
+ <div className="flex gap-2 ml-auto">
+ <RegenerateRecoveryCodesModal />
+ <UnenrollRecoveryCodesModal />
+ </div>
+ </CardContent>
+ </Card>
+ )}
+ </PageSectionContent>
+ </PageSection>
+ )}
+
<AddNewFactorModal
visible={isAddNewFactorOpen}
onClose={() => setIsAddNewFactorOpen(false)}
/>
+
<DeleteFactorModal
visible={factorToBeDeleted !== null}
factorId={factorToBeDeleted}
lastFactorToBeDeleted={totpFactors.length === 1}
onClose={() => setFactorToBeDeleted(null)}
- hasRecoveryCodes={recoveryCodesStatusQuery.data?.status === 'available'}
+ hasRecoveryCodes={status === 'available'}
/>
</>
)