mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
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:
@@ -0,0 +1,53 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { appLockSupported, biometryAvailable, isAppLockEnabled, setAppLockEnabled, unlockApp } from './appLock';
|
||||
import { hasNativeFeature } from './capabilities';
|
||||
import { isNativeApp } from './platform';
|
||||
import { TrekShell } from './trekShell';
|
||||
|
||||
vi.mock('./platform', () => ({ isNativeApp: vi.fn() }));
|
||||
vi.mock('./capabilities', () => ({ hasNativeFeature: vi.fn() }));
|
||||
vi.mock('./trekShell', () => ({
|
||||
TrekShell: { setPrivacyCover: vi.fn(), biometryInfo: vi.fn(), authenticateBiometric: vi.fn() },
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
window.localStorage.clear();
|
||||
vi.mocked(isNativeApp).mockReturnValue(true);
|
||||
vi.mocked(hasNativeFeature).mockReturnValue(true);
|
||||
vi.mocked(TrekShell.setPrivacyCover).mockReset().mockResolvedValue();
|
||||
vi.mocked(TrekShell.biometryInfo).mockReset().mockResolvedValue({ available: true, type: 'faceID' });
|
||||
vi.mocked(TrekShell.authenticateBiometric).mockReset().mockResolvedValue({ ok: true });
|
||||
});
|
||||
|
||||
describe('appLock', () => {
|
||||
it('is only offered in a binary with the feature', () => {
|
||||
expect(appLockSupported()).toBe(true);
|
||||
vi.mocked(hasNativeFeature).mockReturnValue(false);
|
||||
expect(appLockSupported()).toBe(false);
|
||||
});
|
||||
|
||||
it('keeps the switch on the device and tells the native side for the app-switcher cover', async () => {
|
||||
expect(isAppLockEnabled()).toBe(false);
|
||||
await setAppLockEnabled(true);
|
||||
expect(isAppLockEnabled()).toBe(true);
|
||||
expect(TrekShell.setPrivacyCover).toHaveBeenLastCalledWith({ enabled: true });
|
||||
await setAppLockEnabled(false);
|
||||
expect(isAppLockEnabled()).toBe(false);
|
||||
expect(TrekShell.setPrivacyCover).toHaveBeenLastCalledWith({ enabled: false });
|
||||
});
|
||||
|
||||
it('reads off as off without the feature, whatever is stored', async () => {
|
||||
await setAppLockEnabled(true);
|
||||
vi.mocked(hasNativeFeature).mockReturnValue(false);
|
||||
expect(isAppLockEnabled()).toBe(false);
|
||||
expect(await biometryAvailable()).toEqual({ available: false, type: 'none' });
|
||||
});
|
||||
|
||||
it('unlocks on success and stays locked on a cancel or a failure', async () => {
|
||||
expect(await unlockApp('Unlock TREK')).toBe(true);
|
||||
vi.mocked(TrekShell.authenticateBiometric).mockRejectedValue({ code: 'CANCELLED' });
|
||||
expect(await unlockApp('Unlock TREK')).toBe(false);
|
||||
vi.mocked(TrekShell.authenticateBiometric).mockRejectedValue({ code: 'FAILED' });
|
||||
expect(await unlockApp('Unlock TREK')).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,63 @@
|
||||
import { hasNativeFeature } from './capabilities';
|
||||
import { isNativeApp } from './platform';
|
||||
import { reportNativeFailure } from './reportFailure';
|
||||
import { TrekShell } from './trekShell';
|
||||
|
||||
/**
|
||||
* The app lock: Face ID, Touch ID or the device passcode before TREK shows
|
||||
* anything, at launch and after a while in the background, with a blurred
|
||||
* cover in the app switcher meanwhile.
|
||||
*
|
||||
* The setting belongs to the device, not the account (another phone of the
|
||||
* same user may well not want it), so it lives in local storage, and the
|
||||
* native side keeps its own copy for the app-switcher cover, which has to be
|
||||
* up before the UI could run.
|
||||
*/
|
||||
const KEY = 'trek.appLock';
|
||||
/** Back from the background after this long, the app asks again. */
|
||||
export const RELOCK_AFTER_MS = 30_000;
|
||||
|
||||
export function appLockSupported(): boolean {
|
||||
return isNativeApp() && hasNativeFeature('biometricLock');
|
||||
}
|
||||
|
||||
export function isAppLockEnabled(): boolean {
|
||||
if (!appLockSupported()) return false;
|
||||
try {
|
||||
return window.localStorage.getItem(KEY) === '1';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function setAppLockEnabled(enabled: boolean): Promise<void> {
|
||||
try {
|
||||
if (enabled) window.localStorage.setItem(KEY, '1');
|
||||
else window.localStorage.removeItem(KEY);
|
||||
} catch {
|
||||
// Storage refused: the lock then stays off, which the toggle shows.
|
||||
}
|
||||
await TrekShell.setPrivacyCover({ enabled }).catch(reportNativeFailure);
|
||||
}
|
||||
|
||||
export async function biometryAvailable(): Promise<{ available: boolean; type: string }> {
|
||||
if (!appLockSupported()) return { available: false, type: 'none' };
|
||||
try {
|
||||
return await TrekShell.biometryInfo();
|
||||
} catch (err) {
|
||||
reportNativeFailure(err);
|
||||
return { available: false, type: 'none' };
|
||||
}
|
||||
}
|
||||
|
||||
/** Asks for Face ID, Touch ID or the passcode. True once unlocked. */
|
||||
export async function unlockApp(reason: string): Promise<boolean> {
|
||||
try {
|
||||
await TrekShell.authenticateBiometric({ reason });
|
||||
return true;
|
||||
} catch (err) {
|
||||
const code = (err as { code?: string } | null)?.code;
|
||||
if (code !== 'CANCELLED' && code !== 'FAILED') reportNativeFailure(err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,12 @@ export interface TrekShellPlugin {
|
||||
getTileCacheUsage(): Promise<{ bytes: number; limit: number }>;
|
||||
/** iOS, feature tileCache: empties the map tile store. */
|
||||
clearTileCache(): Promise<void>;
|
||||
/** Feature biometricLock: which biometry the device offers for the app lock. */
|
||||
biometryInfo(): Promise<{ available: boolean; type: string }>;
|
||||
/** Feature biometricLock: Face ID, Touch ID or the passcode. Rejects with CANCELLED or FAILED. */
|
||||
authenticateBiometric(options: { reason: string }): Promise<{ ok: true }>;
|
||||
/** Feature biometricLock: a blurred cover in the app switcher while the lock is on. */
|
||||
setPrivacyCover(options: { enabled: boolean }): Promise<void>;
|
||||
}
|
||||
|
||||
export const TrekShell = registerPlugin<TrekShellPlugin>('TrekShell');
|
||||
|
||||
Reference in New Issue
Block a user