From dbcdecb6a7dd9d4dda88e9b2f089e4820928a350 Mon Sep 17 00:00:00 2001 From: Maurice Date: Wed, 2 Dec 2026 10:10:25 +0100 Subject: [PATCH] Give password recovery the native sign-in layout --- client/src/pages/ForgotPasswordPage.test.tsx | 32 +++++- client/src/pages/ForgotPasswordPage.tsx | 6 +- client/src/pages/ResetPasswordPage.test.tsx | 21 +++- client/src/pages/ResetPasswordPage.tsx | 6 +- .../forgotPassword/AppForgotPassword.tsx | 61 ++++++++++++ .../pages/forgotPassword/useForgotPassword.ts | 14 ++- client/src/pages/login/AppAuthScreen.tsx | 87 +++++++++++++++++ client/src/pages/login/AppLogin.tsx | 97 +------------------ client/src/pages/login/appLogin.css | 10 +- .../pages/resetPassword/AppResetPassword.tsx | 95 ++++++++++++++++++ 10 files changed, 329 insertions(+), 100 deletions(-) create mode 100644 client/src/pages/forgotPassword/AppForgotPassword.tsx create mode 100644 client/src/pages/login/AppAuthScreen.tsx create mode 100644 client/src/pages/resetPassword/AppResetPassword.tsx diff --git a/client/src/pages/ForgotPasswordPage.test.tsx b/client/src/pages/ForgotPasswordPage.test.tsx index 2474b6b3f..268a4988d 100644 --- a/client/src/pages/ForgotPasswordPage.test.tsx +++ b/client/src/pages/ForgotPasswordPage.test.tsx @@ -9,9 +9,14 @@ import ForgotPasswordPage from './ForgotPasswordPage' // forgotPassword/useForgotPassword.test.ts). The hook is mocked so the form, // the sent screen and the SMTP hint can each be rendered on demand. -const mocks = vi.hoisted(() => ({ forgot: {} as Record })) +const mocks = vi.hoisted(() => ({ forgot: {} as Record, native: false })) vi.mock('./forgotPassword/useForgotPassword', () => ({ useForgotPassword: () => mocks.forgot })) +vi.mock('../native/platform', async (importOriginal) => ({ + ...(await importOriginal()), + isNativeApp: () => mocks.native, +})) +vi.mock('../../native-shell/scenery', () => ({ startScenery: vi.fn(), cameraForPlan: () => [0, 0] })) const navigate = vi.fn((_to: string) => {}) const setEmail = vi.fn((_v: string) => {}) @@ -32,6 +37,7 @@ function setForgot(over: Record = {}) { beforeEach(() => { vi.clearAllMocks() + mocks.native = false setForgot() }) @@ -124,4 +130,28 @@ describe('ForgotPasswordPage', () => { expect(screen.getByRole('heading', { name: 'Check your email' })).toBeInTheDocument() expect(screen.getByText(/console/i)).toBeInTheDocument() }) + + it('keeps the SMTP warning visible throughout native password recovery', () => { + mocks.native = true + setForgot({ smtpConfigured: false }) + const { rerender } = render() + + expect(screen.getByRole('main')).toHaveClass('al-body') + expect(screen.getByRole('textbox', { name: 'Email' })).toHaveAttribute('autocomplete', 'email') + expect(screen.getByText(/hasn't configured SMTP/i)).toBeVisible() + + setForgot({ smtpConfigured: false, submitted: true }) + rerender() + expect(screen.getByText(/hasn't configured SMTP/i)).toBeVisible() + expect(screen.queryByRole('heading', { name: 'Check your email' })).not.toBeInTheDocument() + }) + + it('lets a native user try again after a requester-wide rate limit', () => { + mocks.native = true + setForgot({ submitted: true, deliveryIssue: 'rateLimit' }) + render() + + expect(screen.getByRole('alert')).toHaveTextContent('Too many attempts') + expect(screen.getByRole('button', { name: 'Send reset link' })).toBeEnabled() + }) }) diff --git a/client/src/pages/ForgotPasswordPage.tsx b/client/src/pages/ForgotPasswordPage.tsx index 95e15e106..ea9287c75 100644 --- a/client/src/pages/ForgotPasswordPage.tsx +++ b/client/src/pages/ForgotPasswordPage.tsx @@ -1,6 +1,8 @@ import React, { ChangeEvent } from 'react' import { Mail, ArrowLeft, CheckCircle2, Terminal } from 'lucide-react' import { useTranslation } from '../i18n' +import { isNativeApp } from '../native/platform' +import AppForgotPassword from './forgotPassword/AppForgotPassword' import { useForgotPassword } from './forgotPassword/useForgotPassword' const inputBase: React.CSSProperties = { @@ -13,7 +15,9 @@ const inputBase: React.CSSProperties = { function ForgotPasswordPage() { const { t } = useTranslation() // Page = wiring container: form state, the SMTP probe and submit live in the hook. - const { navigate, email, setEmail, submitted, isLoading, smtpConfigured, handleSubmit } = useForgotPassword() + const forgot = useForgotPassword() + const { navigate, email, setEmail, submitted, isLoading, smtpConfigured, handleSubmit } = forgot + if (isNativeApp()) return return (
({ reset: {} as Record })) +const mocks = vi.hoisted(() => ({ reset: {} as Record, native: false })) vi.mock('./resetPassword/useResetPassword', () => ({ useResetPassword: () => mocks.reset })) +vi.mock('../native/platform', async (importOriginal) => ({ + ...(await importOriginal()), + isNativeApp: () => mocks.native, +})) +vi.mock('../../native-shell/scenery', () => ({ startScenery: vi.fn(), cameraForPlan: () => [0, 0] })) const navigate = vi.fn((_to: string) => {}) const setPw = vi.fn((_v: string) => {}) @@ -43,10 +48,24 @@ function setReset(over: Record = {}) { beforeEach(() => { vi.clearAllMocks() + mocks.native = false setReset() }) describe('ResetPasswordPage', () => { + it('announces reset errors and supports backup codes on the native screen', () => { + mocks.native = true + setReset({ mfaRequired: true, error: 'The security code was rejected' }) + render() + + expect(screen.getByRole('main')).toHaveClass('al-body') + expect(screen.getByRole('alert')).toHaveTextContent('The security code was rejected') + const code = screen.getByRole('textbox', { name: '2FA code' }) + expect(code).toHaveAttribute('autocomplete', 'one-time-code') + fireEvent.change(code, { target: { value: 'ABCD-EFGH' } }) + expect(setMfaCode).toHaveBeenCalledWith('ABCD-EFGH') + }) + it('FE-PAGE-RESETUI-001: renders the password form for a valid token', () => { setReset() render() diff --git a/client/src/pages/ResetPasswordPage.tsx b/client/src/pages/ResetPasswordPage.tsx index 69a0d4f48..f213938d1 100644 --- a/client/src/pages/ResetPasswordPage.tsx +++ b/client/src/pages/ResetPasswordPage.tsx @@ -1,6 +1,8 @@ import React, { ChangeEvent } from 'react' import { Lock, KeyRound, CheckCircle2, AlertTriangle, Eye, EyeOff } from 'lucide-react' import { useTranslation } from '../i18n' +import { isNativeApp } from '../native/platform' +import AppResetPassword from './resetPassword/AppResetPassword' import { useResetPassword } from './resetPassword/useResetPassword' import PasswordChecklist from '../components/shared/PasswordChecklist' @@ -14,12 +16,14 @@ const inputBase: React.CSSProperties = { function ResetPasswordPage() { const { t } = useTranslation() // Page = wiring container: token, form state, validation + submit live in the hook. + const reset = useResetPassword() const { navigate, token, pw, setPw, pw2, setPw2, showPw, setShowPw, mfaCode, setMfaCode, mfaRequired, error, success, isLoading, handleSubmit, - } = useResetPassword() + } = reset + if (isNativeApp()) return const shell = (inner: React.ReactNode) => (
}) { + const { t } = useTranslation(); + const back = () => void forgot.navigate('/login'); + const smtpOff = forgot.smtpConfigured === false; + const smtpNotice = smtpOff && {t('login.forgotPasswordSmtpHintOff')}; + + return ( + + {forgot.submitted && !forgot.deliveryIssue ? ( +
+
+ {!smtpOff &&
+ {smtpNotice} + +
+ ) : ( + <> +
void forgot.handleSubmit(event)}> +

{t('login.forgotPasswordTitle')}

+

{t('login.forgotPasswordBody')}

+ {smtpNotice} + {forgot.deliveryIssue && ( + + {t(forgot.deliveryIssue === 'rateLimit' ? 'common.tooManyAttempts' : 'native.connect.errorUnreachable')} + + )} + forgot.setEmail(event.target.value)} + placeholder={t('login.emailPlaceholder')} + required + /> + + + + + )} +
+ ); +} diff --git a/client/src/pages/forgotPassword/useForgotPassword.ts b/client/src/pages/forgotPassword/useForgotPassword.ts index 9c9740541..aba9c5b45 100644 --- a/client/src/pages/forgotPassword/useForgotPassword.ts +++ b/client/src/pages/forgotPassword/useForgotPassword.ts @@ -1,5 +1,6 @@ import { useState, useEffect, type FormEvent } from 'react' import { useNavigate } from 'react-router' +import { isAxiosError } from 'axios' import { authApi } from '../../api/client' /** @@ -14,6 +15,7 @@ export function useForgotPassword() { const [submitted, setSubmitted] = useState(false) const [isLoading, setIsLoading] = useState(false) const [smtpConfigured, setSmtpConfigured] = useState(null) + const [deliveryIssue, setDeliveryIssue] = useState<'connection' | 'rateLimit' | null>(null) useEffect(() => { // Probe whether SMTP is configured so we can warn the user up-front that the @@ -29,15 +31,21 @@ export function useForgotPassword() { const handleSubmit = async (e: FormEvent) => { e.preventDefault() if (isLoading) return + setDeliveryIssue(null) setIsLoading(true) try { await authApi.forgotPassword({ email: email.trim() }) - } catch { - // Enumeration-safe: success UX regardless of server outcome. + } catch (error) { + // These errors apply to the connection or requester, not to the mailbox. + // Keep every other outcome indistinguishable from an accepted request. + if (isAxiosError(error)) { + if (error.response?.status === 429) setDeliveryIssue('rateLimit') + else if (!error.response) setDeliveryIssue('connection') + } } setSubmitted(true) setIsLoading(false) } - return { navigate, email, setEmail, submitted, isLoading, smtpConfigured, handleSubmit } + return { navigate, email, setEmail, submitted, isLoading, smtpConfigured, deliveryIssue, handleSubmit } } diff --git a/client/src/pages/login/AppAuthScreen.tsx b/client/src/pages/login/AppAuthScreen.tsx new file mode 100644 index 000000000..b6440a20a --- /dev/null +++ b/client/src/pages/login/AppAuthScreen.tsx @@ -0,0 +1,87 @@ +import { AlertCircle, Globe, Lock } from 'lucide-react'; +import { useEffect, useRef, type InputHTMLAttributes, type ReactNode } from 'react'; +import { useNativeServer } from '../../components/Settings/useNativeServer'; +import { useTranslation } from '../../i18n'; +import { lightStatusBarIcons } from '../../native/appBoot'; +import { reportNativeFailure } from '../../native/reportFailure'; +import './appLogin.css'; + +export function AppAuthScreen({ children, leaving }: { children: ReactNode; leaving?: boolean }) { + useEffect(() => lightStatusBarIcons(), []); + return ( +
+ +
{children}
+
+ ); +} + +function AppAuthHero() { + const { t } = useTranslation(); + const { host, changeServer } = useNativeServer(); + const hero = useRef(null); + + useEffect(() => { + const box = hero.current; + if (!box) return; + let cancelled = false; + // Keep the landscape in the position where the address screen leaves it. + void import('../../../native-shell/scenery') + .then(({ startScenery, cameraForPlan }) => { + if (!cancelled) startScenery(box, { frame: 320, camera: cameraForPlan(1), haze: false }); + }) + .catch(reportNativeFailure); + return () => { + cancelled = true; + box.querySelector('canvas.scenery')?.remove(); + }; + }, []); + + return ( +
+ TREK + {host && ( +
+
+ + +
+
+
+ +
+ )} +
+ ); +} + +export function Notice({ tone, children }: { tone?: 'warn'; children: ReactNode }) { + return ( +
+
+ ); +} + +export function Field({ + label, + after, + ...input +}: { label: string; after?: ReactNode } & InputHTMLAttributes) { + return ( +
+ + {after && {after}} +
+ ); +} diff --git a/client/src/pages/login/AppLogin.tsx b/client/src/pages/login/AppLogin.tsx index 399ad70d8..ef485e7df 100644 --- a/client/src/pages/login/AppLogin.tsx +++ b/client/src/pages/login/AppLogin.tsx @@ -1,12 +1,8 @@ -import { AlertCircle, Eye, EyeOff, Fingerprint, Globe, Lock, Shield } from 'lucide-react'; -import React, { useEffect, useRef } from 'react'; +import { Eye, EyeOff, Fingerprint, Shield } from 'lucide-react'; import ToggleSwitch from '../../components/Settings/ToggleSwitch'; -import { useNativeServer } from '../../components/Settings/useNativeServer'; import PasswordChecklist from '../../components/shared/PasswordChecklist'; import { useTranslation } from '../../i18n'; -import { lightStatusBarIcons } from '../../native/appBoot'; -import { reportNativeFailure } from '../../native/reportFailure'; -import './appLogin.css'; +import { AppAuthScreen, Field, Notice } from './AppAuthScreen'; import { heading as headingOf, oidcLoginHref, oidcOnlyHref, submitLabel, type LoginStep } from './loginModel'; import { mfaCodeInputProps } from './mfaCodeInput'; import { IDP_LOGIN_URL, type useLogin } from './useLogin'; @@ -22,14 +18,10 @@ type Login = ReturnType; * (the device decides, see useLogin) differ. */ export default function AppLogin({ login }: { login: Login }) { - useEffect(() => lightStatusBarIcons(), []); return ( -
- -
- -
-
+ + + ); } @@ -59,85 +51,6 @@ function AppLoginContent({ login }: { login: Login }) { return ; } -/** The landscape of the address screen, with the server this screen signs in to. */ -function AppLoginHero() { - const { t } = useTranslation(); - const { host, changeServer } = useNativeServer(); - const hero = useRef(null); - - useEffect(() => { - const box = hero.current; - if (!box) return; - let cancelled = false; - // The same camera the address screen ends on, so the picture holds still - // across the change of screens. The scene stops drawing once its canvas - // has left the page. - void import('../../../native-shell/scenery') - .then(({ startScenery, cameraForPlan }) => { - if (!cancelled) startScenery(box, { frame: 320, camera: cameraForPlan(1), haze: false }); - }) - // Without the landscape the box keeps its plain sea colour. - .catch(reportNativeFailure); - return () => { - cancelled = true; - box.querySelector('canvas.scenery')?.remove(); - }; - }, []); - - return ( -
- TREK - {host && ( -
-
- - -
-
-
- -
- )} -
- ); -} - -function Notice({ tone, children }: { tone?: 'warn'; children: React.ReactNode }) { - return ( -
-
- ); -} - -/** - * A field on a white card, the label above the input, as on the address screen. - * A control beside the input (the password's eye) sits outside the label, so - * the label names the input alone. - */ -function Field({ - label, - after, - ...input -}: { label: string; after?: React.ReactNode } & React.InputHTMLAttributes) { - return ( -
- - {after && {after}} -
- ); -} - function OidcOnly({ login }: { login: Login }) { const { t } = useTranslation(); return ( diff --git a/client/src/pages/login/appLogin.css b/client/src/pages/login/appLogin.css index 34c34a09f..ff5601332 100644 --- a/client/src/pages/login/appLogin.css +++ b/client/src/pages/login/appLogin.css @@ -1,5 +1,5 @@ /* - * The sign-in screen inside the iOS and Android app (AppLogin.tsx). It is the + * The sign-in and password recovery screens inside the iOS and Android app. The sign-in is the * next screen after the app's own address screen (native-shell/), so it wears * that screen's look: the same landscape on top, the same glass card with the * server on it, the same type, fields and buttons on the same warm page. The @@ -295,6 +295,14 @@ line-height: 1.5; color: var(--al-text); } +.al-recovery-feedback { + display: grid; + gap: 8px; + justify-items: start; +} +.al-recovery-icon { + color: var(--al-good); +} /* A field on a white card: the label above, the input below it. */ .al-fld { diff --git a/client/src/pages/resetPassword/AppResetPassword.tsx b/client/src/pages/resetPassword/AppResetPassword.tsx new file mode 100644 index 000000000..773291bbb --- /dev/null +++ b/client/src/pages/resetPassword/AppResetPassword.tsx @@ -0,0 +1,95 @@ +import { CheckCircle2, Eye, EyeOff } from 'lucide-react'; +import PasswordChecklist from '../../components/shared/PasswordChecklist'; +import { useTranslation } from '../../i18n'; +import { AppAuthScreen, Field, Notice } from '../login/AppAuthScreen'; +import { mfaCodeInputProps } from '../login/mfaCodeInput'; +import type { useResetPassword } from './useResetPassword'; + +type Reset = ReturnType; + +export default function AppResetPassword({ reset }: { reset: Reset }) { + const { t } = useTranslation(); + return ( + + {reset.success ? ( +
+
+
+ +
+ ) : !reset.token ? ( +
+

{t('login.resetPasswordInvalidLink')}

+ {t('login.resetPasswordInvalidLinkBody')} + +
+ ) : ( + + )} +
+ ); +} + +function ResetForm({ reset }: { reset: Reset }) { + const { t } = useTranslation(); + const inputType = reset.showPw ? 'text' : 'password'; + const title = reset.mfaRequired ? t('login.mfaTitle') : t('login.resetPasswordTitle'); + const action = reset.mfaRequired ? t('login.resetPasswordVerify') : t('login.resetPasswordSubmit'); + return ( +
void reset.handleSubmit(event)}> +

{title}

+

{t(reset.mfaRequired ? 'login.resetPasswordMfaBody' : 'login.resetPasswordBody')}

+ {reset.error && {reset.error}} + {reset.mfaRequired ? ( + + ) : ( + <> + reset.setPw(event.target.value)} + required + after={ + + } + /> + + reset.setPw2(event.target.value)} + required + /> + + )} + + + ); +}