Secure Storage & Secrets

Store tokens and sensitive data in the Keychain or Keystore, keep secrets out of the app bundle, and protect data at rest.

Intermediate · about 4 hours · difficulty 3/5

Learn first: Local Storage: Key-value, SQLite & Files, Authentication & Session Handling

Assume your app binary will be downloaded, unzipped and inspected, and that some devices are rooted or jailbroken. Anything shipped inside the app is effectively public.

Where sensitive data goes

  • Tokens, passwords and keys: the platform secure store. Keychain on iOS and Keystore-backed encryption on Android. Wrappers: expo-secure-store, react-native-keychain, flutter_secure_storage, Android's EncryptedSharedPreferences successors or DataStore with Tink.
  • Non-sensitive preferences: AsyncStorage, SharedPreferences, UserDefaults. These are plain files.
  • Databases with personal data: consider SQLCipher or encrypted Realm, and whether you need to store that data at all.
import * as SecureStore from "expo-secure-store";
await SecureStore.setItemAsync("refreshToken", token);

Secrets do not belong in the app

API keys for paid services, database credentials and signing keys must stay on your server. The app should call your backend, which holds the secret. Public client keys (Firebase config, Maps keys) are fine to ship but restrict them by package name, bundle id and API scope.

Other data-at-rest risks

  • Logs that print tokens or personal data (strip them in release builds).
  • Screenshots in the app switcher showing sensitive screens (blur or secure-flag them).
  • Clipboard use for passwords and codes.
  • Backups: exclude secrets from cloud backups where appropriate.

Biometrics

Use biometrics to unlock a key held in the secure store (access control flags), not as a simple yes/no check in JavaScript that could be bypassed.

Good enough to move on: your auth tokens live only in secure storage, no private secrets appear in the bundle, and sensitive screens are hidden in the app switcher.

Key concepts

  • Keychain and Keystore
  • expo-secure-store and equivalents
  • Why app bundles cannot hold secrets
  • Restricting public API keys
  • Encrypted databases
  • Log and screenshot hygiene
  • Biometric-bound keys

Mini project

Refactor an app that stores tokens in plain storage to use secure storage, move a third-party API key behind a small backend proxy, and blur the app in the task switcher.

Check yourself

A teammate wants to put a payment provider secret key in an environment variable bundled with the app. What do you tell them?

Free resources