feat(native): an app lock with Face ID, Touch ID or the device passcode

A switch in the account settings, inside the app on a device that can unlock
that way (iOS LocalAuthentication, Android BiometricPrompt with the screen
lock as fallback). Turning it on asks once, so nobody locks themselves out.
With it on, TREK asks at launch and after half a minute in the background
before showing anything, and the app switcher shows a blurred cover (iOS) or
no snapshot (Android) instead of the screen. The setting stays on the
device, not the account.
This commit is contained in:
Maurice
2026-11-06 13:21:19 +01:00
parent dec6babe1d
commit 017f229cfc
42 changed files with 721 additions and 2 deletions
@@ -0,0 +1,55 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen, waitFor } from '../../../../tests/helpers/render';
import {
appLockSupported,
biometryAvailable,
isAppLockEnabled,
setAppLockEnabled,
unlockApp,
} from '../../../native/appLock';
import MAppLockCard from './MAppLockCard';
vi.mock('../../../native/appLock', () => ({
appLockSupported: vi.fn(),
biometryAvailable: vi.fn(),
isAppLockEnabled: vi.fn(),
setAppLockEnabled: vi.fn(async () => {}),
unlockApp: vi.fn(),
}));
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(appLockSupported).mockReturnValue(true);
vi.mocked(biometryAvailable).mockResolvedValue({ available: true, type: 'faceID' });
vi.mocked(isAppLockEnabled).mockReturnValue(false);
vi.mocked(unlockApp).mockResolvedValue(true);
});
describe('MAppLockCard', () => {
it('turns the lock on only after one successful unlock', async () => {
render(<MAppLockCard />);
const toggle = await screen.findByRole('switch');
vi.mocked(unlockApp).mockResolvedValueOnce(false);
fireEvent.click(toggle);
await waitFor(() => expect(unlockApp).toHaveBeenCalledTimes(1));
expect(setAppLockEnabled).not.toHaveBeenCalled();
fireEvent.click(toggle);
await waitFor(() => expect(setAppLockEnabled).toHaveBeenCalledWith(true));
});
it('turns it off without asking', async () => {
vi.mocked(isAppLockEnabled).mockReturnValue(true);
render(<MAppLockCard />);
fireEvent.click(await screen.findByRole('switch'));
await waitFor(() => expect(setAppLockEnabled).toHaveBeenCalledWith(false));
expect(unlockApp).not.toHaveBeenCalled();
});
it('is not offered on a device without Face ID, Touch ID or a passcode', async () => {
vi.mocked(biometryAvailable).mockResolvedValue({ available: false, type: 'none' });
const { container } = render(<MAppLockCard />);
await waitFor(() => expect(biometryAvailable).toHaveBeenCalled());
expect(container).toBeEmptyDOMElement();
});
});
@@ -0,0 +1,54 @@
import { ScanFace } from 'lucide-react';
import { useEffect, useState } from 'react';
import { useTranslation } from '../../../i18n';
import {
appLockSupported,
biometryAvailable,
isAppLockEnabled,
setAppLockEnabled,
unlockApp,
} from '../../../native/appLock';
import MToggle from '../../components/MToggle';
import { MSetCard, MSetRow } from './MSettingsUi';
/**
* The app lock switch, inside the app on a device that can unlock with Face ID,
* Touch ID or its passcode. Turning it on asks once, so nobody locks themselves
* out with a lock that does not work on this phone.
*/
export default function MAppLockCard() {
const { t } = useTranslation();
const [available, setAvailable] = useState(false);
const [enabled, setEnabled] = useState(isAppLockEnabled);
useEffect(() => {
if (!appLockSupported()) return;
void biometryAvailable().then((info) => setAvailable(info.available));
}, []);
if (!available) return null;
const toggle = async () => {
if (enabled) {
await setAppLockEnabled(false);
setEnabled(false);
return;
}
if (!(await unlockApp(t('native.lock.reason')))) return;
await setAppLockEnabled(true);
setEnabled(true);
};
return (
<MSetCard title={t('native.lock.settingTitle')} icon={ScanFace} className="mt-3">
<div className="-mt-[4px]">
<MSetRow
first
label={t('native.lock.setting')}
sub={t('native.lock.settingHint')}
trailing={<MToggle checked={enabled} onChange={() => void toggle()} ariaLabel={t('native.lock.setting')} />}
/>
</div>
</MSetCard>
);
}
@@ -19,6 +19,7 @@ import {
import { useTranslation } from '../../../i18n'
import { MSetCard, MSetEyebrow, MSetInput, MSetButton, MSetHint } from './MSettingsUi'
import MConfirmSheet from './MConfirmSheet'
import MAppLockCard from './MAppLockCard'
import PasswordChecklist from '../../../components/shared/PasswordChecklist'
import { fmtDate, usePasskeys } from '../../../components/Settings/usePasskeys'
import { stripTrailingSlashes, useAccountSettings } from '../../../components/Settings/useAccountSettings'
@@ -179,6 +180,9 @@ export default function MSettingsAccount() {
)}
{/* ── Two-factor authentication ───────────────────────────── */}
{/* The app lock, inside the app only (Face ID, Touch ID or the passcode). */}
<MAppLockCard />
<MSetCard title={t('settings.mfa.title')} icon={KeyRound} className="mt-3">
{mfaRequiredByPolicy && (
<div className="mb-3 flex gap-2 rounded-xl border border-[color:var(--m-rowbr)] bg-[color:var(--m-ic)] p-3">