Open-source personal finance manager. Self-hosted, privacy-first.
fix(2fa): report the real reason a 2FA disable failed (#891)
Albert Dolkun5f5c22d72b8e
diff --git a/frontend/src/components/two-factor-setup.test.tsx b/frontend/src/components/two-factor-setup.test.tsxnew file mode 100644index 00000000..665af2c2--- /dev/null+++ b/frontend/src/components/two-factor-setup.test.tsx@@ -0,0 +1,52 @@+import { beforeEach, describe, expect, it, vi } from 'vitest'+import { screen } from '@testing-library/react'+import type { AxiosError } from 'axios'++import { TwoFactorSetup } from '@/components/two-factor-setup'+import { renderWithProviders, t } from '@/test/utils'++const authContext = vi.hoisted(() => ({+ user: { is_2fa_enabled: true },+ updateUser: vi.fn(),+}))+vi.mock('@/contexts/auth-context', () => ({ useAuth: () => authContext }))++const api = vi.hoisted(() => ({ auth: { disable2fa: vi.fn() } }))+vi.mock('@/lib/api', () => ({ auth: api.auth }))++function detailError(detail: string): AxiosError<{ detail: string }> {+ return {+ isAxiosError: true,+ response: { status: 400, data: { detail } },+ } as AxiosError<{ detail: string }>+}++async function submitDisable() {+ const { user } = renderWithProviders(<TwoFactorSetup open onClose={vi.fn()} />)+ await user.type(screen.getByLabelText(t('auth.password')), 'wrong-password')+ await user.type(screen.getByLabelText(t('auth.twoFactor')), '123456')+ await user.click(screen.getByRole('button', { name: t('auth.disable2fa') }))+}++beforeEach(() => {+ vi.clearAllMocks()+})++describe('TwoFactorSetup disable flow', () => {+ it('blames the password when the backend rejected the password', async () => {+ api.auth.disable2fa.mockRejectedValue(detailError('Invalid password'))++ await submitDisable()++ expect(await screen.findByRole('alert')).toHaveTextContent(t('auth.currentPasswordWrong'))+ expect(screen.queryByText(t('auth.invalid2faCode'))).not.toBeInTheDocument()+ })++ it('blames the connection when the server never answered', async () => {+ api.auth.disable2fa.mockRejectedValue({ isAxiosError: true } as AxiosError)++ await submitDisable()++ expect(await screen.findByRole('alert')).toHaveTextContent(t('auth.serverError'))+ })+})diff --git a/frontend/src/components/two-factor-setup.tsx b/frontend/src/components/two-factor-setup.tsxindex 4f38f5f3..8e55a5bc 100644--- a/frontend/src/components/two-factor-setup.tsx+++ b/frontend/src/components/two-factor-setup.tsx@@ -1,9 +1,11 @@import { useState } from 'react'+import type { AxiosError } from 'axios'import { useTranslation } from 'react-i18next'import { toast } from 'sonner'import { QRCodeSVG } from 'qrcode.react'import { useAuth } from '@/contexts/auth-context'import { auth } from '@/lib/api'+import { isServerUnreachable } from '@/lib/auth-errors'import {Dialog,DialogContent,@@ -78,8 +80,12 @@ export function TwoFactorSetup({ open, onClose }: TwoFactorSetupProps) {toast.success(t('auth.twoFactorDisabled'))if (user) updateUser({ ...user, is_2fa_enabled: false })handleClose()- } catch {- setError(t('auth.invalid2faCode'))+ } catch (err) {+ const detail = (err as AxiosError<{ detail?: string }>).response?.data?.detail+ if (isServerUnreachable(err)) setError(t('auth.serverError'))+ else if (detail === 'Invalid password') setError(t('auth.currentPasswordWrong'))+ else if (detail === 'Invalid 2FA code') setError(t('auth.invalid2faCode'))+ else setError(t('common.error'))} finally {setDisableLoading(false)}@@ -111,6 +117,7 @@ export function TwoFactorSetup({ open, onClose }: TwoFactorSetupProps) {<Inputid="disable-password"type="password"+ autoComplete="current-password"value={disablePassword}onChange={(e) => setDisablePassword(e.target.value)}required@@ -122,6 +129,7 @@ export function TwoFactorSetup({ open, onClose }: TwoFactorSetupProps) {id="disable-code"type="text"inputMode="numeric"+ autoComplete="one-time-code"value={disableCode}onChange={(e) => setDisableCode(e.target.value.replace(/\D/g, '').slice(0, 6))}placeholder="000000"@@ -130,7 +138,11 @@ export function TwoFactorSetup({ open, onClose }: TwoFactorSetupProps) {required/></div>- {error && <p className="text-sm text-destructive">{error}</p>}+ {error && (+ <p role="alert" className="text-sm text-destructive">+ {error}+ </p>+ )}<DialogFooter><Button type="button" variant="outline" onClick={handleClose}>{t('common.cancel')}