> **Sole author (personal open-source library)** · June 2026 to present
> Source: https://www.simonbetton.com/case-studies/expo-device-auth.md (markdown only; there is no HTML page for this document).
> Index of case studies: https://www.simonbetton.com/case-studies/index.md

# Case study: expo-device-auth, a local device authentication gate

## Quick facts

|  |  |
| --- | --- |
| **Role** | Sole author (personal open-source library) |
| **Product** | expo-device-auth, an MIT-licensed Expo / React Native library, public on GitHub |
| **Timeline** | June 2026 to present |
| **Team** | One |
| **Audience** | Expo apps that need a biometric or PIN prompt in front of a sensitive action |
| **Stack** | Expo SDK 55, React Native 0.83, React 19.2, TypeScript 5.9, Expo LocalAuthentication and SecureStore, react-native-quick-crypto over Nitro |
| **Status** | Pre-1.0, MIT, source on GitHub, registry publication to follow; 10 Jest suites, 62 tests, 90% coverage gate enforced in CI; one example app |

## Overview

expo-device-auth is a local device gate for Expo apps. A host app wraps its tree in a provider, then asks for authentication, or for a fresh prompt, before a sensitive action. Biometrics come from Expo. The PIN fallback, lockouts, session window, and default PIN screens belong to the package.

The work is the contract with host apps: what a success result means, what is stored on the device, and which problems stay out of scope. The whole domain runs and is tested on Node, without a device.

## The problem

React Native apps often need a second check on the device after the user is already signed in: confirm a transfer, reveal a secret, unlock a wallet-style screen. That prompt is not login. It is a local unlocked window that should expire, survive or die across process restarts on purpose, and fall back to a PIN the app controls.

Most Expo apps stitch this together from LocalAuthentication plus ad hoc SecureStore writes, and the same failures recur. The OS passcode is used as the PIN fallback, which bypasses any app lockout policy. The raw PIN, or a reversible encoding of it, ends up in storage. "Unlock" and "confirm this action" are the same call, so a still-valid local session silently approves a transfer. Forgot PIN becomes "wipe and continue" with no identity check. The UI is either unstyled or locked to a styling framework the host does not use. Tests need a simulator because biometrics, Keychain, and the clock are hard-wired.

The goal was a package that apps can configure, restyle, and test, while staying honest about the threat model. It is not server authorisation, account recovery, or fraud scoring. Those stay with the host.

Constraints I set up front: target Expo SDK 55, React Native 0.83, and React 19.2. Expo Go is not supported because the native modules have to be in the app binary, so the example app uses development builds and the New Architecture. Do not require NativeWind or another styling library. Ship no custom native code in the first version; compose Expo modules and one community crypto module through a config plugin instead, which limits native behaviour to what those modules expose.

## What I decided

Biometrics never fall back to the OS device credential. A failed or cancelled Face ID or fingerprint prompt goes to the package PIN. That keeps lockouts and PIN policy inside the library, at the cost of one more screen the host must accept.

Authenticate may succeed from a still-valid local session. Reauthenticate always prompts. Change PIN requires a valid session first, and both setup and change end by locking the session, so a new PIN is never followed by a free unlock. Setup is rejected if that credential scope already has a PIN; change and recovery are separate flows.

Sessions are in memory by default with a five-minute timeout. A host can persist a finite session across restarts, or use an infinite in-memory session. Infinite plus persist-across-restarts is rejected at configuration time, because that combination is a stored permanent unlock.

Lockout is a ladder: five failures, then 30 seconds; five more, then five minutes; then fifteen minutes; the configured list is climbed and the last step repeats. Failed-attempt state lives next to the verifier in SecureStore, so the host does not invent a locked-until state.

Forgot PIN is hidden unless the host passes a recovery callback. Reset only clears local credentials; the host must already have proved identity by its own means.

The verifier format is versioned. New writes use PBKDF2-HMAC-SHA256. Reads still accept an earlier salted SHA-256 record so a later KDF change does not brick existing installs. On a four-digit PIN, 10,000 values, iteration count alone does not save anyone; lockouts, hardware-backed storage, and never storing the PIN matter more. I still wanted a real password-based KDF so the stored verifier is not a plain hash of a short secret. Native PBKDF2 is the production path so a 100,000-iteration derivation is not a long JavaScript hitch; tests inject a JavaScript driver from noble-hashes, or a fake, so coverage does not depend on Nitro being present.

## How it is built

```
Host app
  └─ DeviceAuthProvider ── useDeviceAuth()
        │  modal PIN prompt, setup / change / entry screens (styles + slots)
        ▼
  Controller (domain)
     PIN setup/change · sessions · lockout ladder · biometric preference · reset
     ports: storage · biometric driver · clock · random bytes · hash driver
        ▼                               ▼
  Expo adapters                     Test doubles
  SecureStore · LocalAuthentication  in-memory storage · fake biometrics
  Quick Crypto (Nitro) PBKDF2/RNG    fixed clock · noble-hashes PBKDF2
```

The controller owns the rules above and talks to everything else through ports. Clock, random bytes, hash driver, and iteration count are injectable, which is what lets the whole domain run on Node. The domain language comes first: an eight-term glossary defines device-auth session, credential scope, PIN verifier, configured PIN, biometric preference, effective biometric availability, reauthentication and forgot-PIN recovery. It keeps a device-auth session separate from the host app's login session, which is the boundary the package exists to hold.

The Expo adapters are the production wiring. SecureStore is storage. LocalAuthentication is the biometric driver, always called with device fallback disabled and the Android security level from config (strong by default). Quick Crypto, reached through Nitro hybrid objects, supplies PBKDF2 and random fill on device; the provider wires both by default, and the verifier's JavaScript fallback throws rather than using a weak generator if no secure random source exists.

The React layer is the product surface. A provider owns a modal PIN prompt for in-place reauthentication, a hook is the host API, and separate screens cover setup, change, and PIN entry for hosts that want routes instead of the modal. The keypad is built from core React Native primitives; hosts restyle through a style map or replace dots, keys, and the forgot-PIN control through slots. Hermes in this React Native line lacks a newer Promise helper, so the modal uses an explicit deferred promise, and that path is tested. Components do not sync state through mount effects; the single mount subscription (lock when the app leaves the active state) is wrapped so screens do not grow effect chains.

The stored record is version, KDF name, iteration count, a 16-byte salt, and a 32-byte derived key, all hex, held in SecureStore under a scoped key. Scope strings are restricted to letters, digits, periods, underscores, and hyphens because they have to be valid SecureStore segments; one app can isolate more than one local auth context, for example per signed-in user. Comparison is a length check followed by a full pass over the hex that accumulates differences instead of returning at the first mismatch.

Enabling biometrics requires a valid local session and, where something is enrolled, a successful biometric prompt. With hardware but no enrolment, the preference can be switched on so it becomes effective after enrolment. Without hardware it is forced off. Disabling the preference currently needs no fresh prompt: a config flag for that policy exists in the public type with a default, but nothing reads it yet.

## Testing without a device

Ten Jest suites, 62 tests, all on Node. Domain tests drive the controller with in-memory storage, a fake biometric driver, a fixed clock, and an injected hash driver, including the lockout ladder. Adapter tests mock Expo and Nitro. Screen tests drive the keypad. Plugin tests check that Face ID copy and Android backup flags are forwarded and that omitted options fall back to conservative defaults. Config tests cover valid scope keys and the rejected infinite-plus-persist combination.

Jest enforces 90% on branches, functions, lines, and statements. Coverage is 98.3% statements, 90.8% branches, 98.3% functions, 98.1% lines, so branch coverage sits within one point of the gate.

The example app is an Expo Router development client with home actions, setup, change, and a forgot-PIN route whose button is labelled as a simulated verified reset and whose copy says a real app would verify identity with its backend first. It is typechecked against the built library rather than source, which catches export and declaration mistakes that unit tests on source would miss.

## Packaging and CI

The repo is a pnpm 11 workspace linking the library and the example app. The library builds with react-native-builder-bob to CommonJS, ESM, and TypeScript declarations; React Native apps resolve source directly and a pack hook builds before publish. Peer ranges are Expo 55 to 56, React Native 0.83 to 0.84, React 19.2 or later.

The config plugin is a run-once composition of expo-local-authentication, expo-secure-store, and react-native-quick-crypto, with Face ID copy and Android backup behaviour as its options.

CI is one GitHub Actions job on Node 24: frozen pnpm install, then coverage, package typecheck, build, and example typecheck.

## Technologies and standards

| Layer | Choices |
| --- | --- |
| App platform | Expo SDK 55, Expo Router, development builds, New Architecture in the example |
| UI runtime | React 19.2, React Native 0.83, TypeScript 5.9 |
| Biometrics | Expo LocalAuthentication; device fallback disabled; Android strong authenticators by default |
| Secret storage | Expo SecureStore; Android backup behaviour set by the plugin |
| Native crypto | react-native-quick-crypto over Nitro for PBKDF2-SHA-256 and random fill |
| JS crypto (tests, injectable) | @noble/hashes PBKDF2-HMAC-SHA256 and SHA-256 |
| Verifier record | Versioned; 16-byte salt; 32-byte key; 100,000 iterations default; legacy salted SHA-256 accepted on read |
| Config plugin | @expo/config-plugins run-once wrapper |
| Build | react-native-builder-bob: CommonJS, ESM, declarations |
| Tests | Jest, ts-jest, Testing Library for React Native; 90% gate on four coverage measures |
| Lint and format | Ultracite over Oxlint and Oxfmt; Lefthook pre-commit |
| CI | GitHub Actions, Ubuntu, Node 24, pnpm 11.3, frozen lockfile |
| Licence | MIT |

PBKDF2 as in RFC 8018, HMAC-SHA-256, and SHA-256 are the referenced primitives. Platform biometrics follow Apple LocalAuthentication and Android BiometricPrompt class strength as Expo exposes them. Nothing here is a certified module or server-side authenticator.

## How AI was used

There is no model in the library. A local PIN verifier should not grow a model dependency, and if a host later puts an agent in front of a transfer, this package is only the gate in front of it; the host should require a fresh prompt before an agent spends money or reveals secrets.

In development, four agent skills are pinned in a lockfile with content hashes: Software Mansion's React Native best practices and its Radon IDE tooling, Fallow, and a skill that keeps components off effect-driven state sync. Pinning means an agent loads a known skill version rather than whatever the network served that day. A Cursor hook runs the formatter after every agent edit so agent output meets the same lint rules as a human save. Fallow runs locally but is not a CI job. The CI gate is the same for human and agent changes: coverage and types, no model involved.

## Status

| Measure | Value |
| --- | --- |
| Tests | 10 suites, 62 tests, all passing |
| Coverage | 98.3% statements, 90.8% branches, 98.3% functions, 98.1% lines against a 90% gate |
| Artifacts | CommonJS, ESM, TypeScript declarations |
| Design records | One ADR (Expo-first, no custom native code) and a glossary |

The library has not yet been exercised by a production host or App Store and Play review, and the require-auth-to-disable policy should be enforced before a 0.1 that a real app depends on.
