# Store Auth Tokens in Secure Storage in a React Native App

Yes. An agent moves your access and refresh tokens from plain MMKV into the device keychain with expo-secure-store, keeps hydration and tests working and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-secure-storage/react-native/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads how the app keeps its session before touching it. In an app built from obytes/react-native-template-obytes, `package.json` is Expo SDK 54 with `expo-router` 6, React Native 0.81, `react-native-mmkv` 4, `zustand` 5, `axios` and `@tanstack/react-query`, managed with pnpm 10. `src/lib/storage.tsx` creates one `createMMKV()` instance with no `encryptionKey` and wraps it in `getItem`, `setItem` and `removeItem` that JSON-encode values. `src/lib/auth/utils.tsx` stores the whole `TokenType`, `{ access, refresh }`, under the key `token` through that same wrapper, so both tokens sit in an unencrypted MMKV file on disk beside the selected theme, the language and the first-launch flag, which also live there and are fine where they are. `src/features/auth/use-auth-store.tsx` is a zustand store with `signIn`, `signOut` and a synchronous `hydrate` that reads the token at startup, and `src/app/_layout.tsx` calls `hydrateAuth()` at module load, before the first render, while `(app)/_layout.tsx` redirects to `/login` when `status` is `signOut`. `login-screen.tsx` signs in with placeholder strings, `src/lib/api/client.tsx` is an axios instance with only a `baseURL`, and `jest-setup.ts` mocks `react-native-mmkv` for the `jest-expo` test suite.

The change keeps the app's shape and moves only the secret. The agent installs `expo-secure-store` with `npx expo install` so the version matches SDK 54, and rewrites `src/lib/auth/utils.tsx` to store the access and refresh tokens under two keys through SecureStore, which uses the iOS Keychain and Android's Keystore-backed encrypted storage. Hydration stays synchronous: SecureStore exposes a synchronous `getItem` alongside the async calls, so `hydrate()` keeps working at module load and the redirect in `(app)/_layout.tsx` still sees the right status on the first frame. `setToken` and `removeToken` become `setItemAsync` and `deleteItemAsync` behind the same names, with `signIn` and `signOut` in the store awaiting them so a sign-out that fails to clear the keychain is reported rather than swallowed. The MMKV wrapper in `src/lib/storage.tsx` is untouched, because theme, language and onboarding state are not secrets.

Two platforms need a word. SecureStore is not available on web, and the template ships `react-native-web` and a `web` script, so the agent adds a small platform split that falls back to the existing MMKV wrapper on web and says so in a comment and the pull request. And SecureStore values have a size limit on iOS, which is why access and refresh are stored under separate keys rather than as one JSON blob. The agent also writes the one piece of plumbing the template leaves open: an axios request interceptor in `src/lib/api/client.tsx` that reads the access token from the store and sets the `Authorization` header, so there is exactly one place the token is read for requests.

Then it proves the change with the repository's own checks. It adds an `expo-secure-store` mock to `jest-setup.ts` backed by an in-memory map, writes a test for `use-auth-store.tsx` that signs in, hydrates a fresh store and signs out, asserting the keychain mock is written and cleared, and runs `pnpm type-check`, `pnpm lint` and `pnpm test`, the same commands the `type-check.yml`, `lint-ts.yml` and `test.yml` workflows run. It runs `npx expo-doctor` as `expo-doctor.yml` does, so the new native module is known to be compatible with the SDK before anyone starts an EAS build.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Move this app's auth tokens into secure storage. Read package.json, src/lib/storage.tsx, src/lib/auth/utils.tsx, src/features/auth/use-auth-store.tsx, src/app/_layout.tsx, src/app/(app)/_layout.tsx, src/lib/api/client.tsx, jest-setup.ts and the workflows under .github/workflows first. Install expo-secure-store with expo install so it matches the SDK, store the access and refresh tokens under separate keys through SecureStore while leaving non-secret preferences in MMKV, keep hydration synchronous with SecureStore's synchronous getter so the first render still redirects correctly, make sign-in and sign-out await the writes and surface failures, and fall back to the existing storage on web with a comment. Add an axios request interceptor that sets the Authorization header from the stored access token. Mock expo-secure-store in jest-setup.ts, write a store test that signs in, hydrates and signs out against the mock, and run type-check, lint, test and expo-doctor. Open a pull request that lists every file touched and the one behaviour difference on web.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that adds `expo-secure-store` to `package.json` and the lockfile, rewrites `src/lib/auth/utils.tsx`, adjusts `signIn` and `signOut` in `src/features/auth/use-auth-store.tsx`, adds the interceptor to `src/lib/api/client.tsx`, extends `jest-setup.ts` with the mock and adds `use-auth-store.test.tsx`. The description explains why hydration stays synchronous, why the two tokens are separate keys, and that web keeps the old storage.

The run's record keeps `pnpm type-check`, `pnpm lint`, `pnpm test` with the new store test and `npx expo-doctor` from the sandbox, the same checks the repository's workflows run on a pull request, so a reviewer sees them pass before CI repeats them.

Before merging, a reviewer builds a development client, signs in on a device, kills and relaunches the app to see the session survive from the keychain, and decides whether existing users' tokens in MMKV should be migrated on first launch or simply require one new sign-in, which the pull request leaves as a question.

## Run it on every Linear issue labelled security

A team that triages in Linear adds an automation with a `linear` trigger on `linear.issue.labeled` for the label `security`, the Project that has this repository, and this prompt. When an engineer labels the issue that says tokens are stored in plain MMKV, one run starts with the issue's text added after the prompt, does the work and opens a pull request that links back to the issue.

The automation fills in no placeholder: the prompt stays as written and the issue follows it, so the same automation handles the next storage issue, such as moving a cached API key, as long as the issue says what to move.

## Why run it on Rush?

Token storage touches the first render, the test setup, the web build and a native module's SDK compatibility at once, which is why it is often left in plain storage. The agent reads all of those in its own sandbox, keeps hydration synchronous so the redirect logic does not change, and runs the repository's own type check, lint, tests and Expo doctor before the pull request exists.

Each check and its output is in the run's record. The reviewer spends their time on the one question that needs a human, whether to migrate existing sessions, rather than on whether the mock and the interceptor work.

## Frequently asked questions

### Why not just set an encryption key on MMKV?

Because the key would then need somewhere secure to live, which is the keychain, so the tokens may as well go there directly. SecureStore uses the iOS Keychain and Android's Keystore-backed storage, and the non-secret preferences stay in MMKV where they are fast and fine.

### Does startup change?

No. `hydrate()` still runs synchronously at module load using SecureStore's synchronous getter, so the tab layout still sees `signIn` or `signOut` on the first frame and redirects as before. Only the writes become asynchronous, inside `signIn` and `signOut`.

### What happens on web?

SecureStore has no web implementation, so a platform split falls back to the existing MMKV wrapper there, with a comment. The pull request names this as the one behaviour difference so the team can decide whether web should keep tokens in memory only.

### Will existing users be signed out?

On the first launch after the update the keychain is empty, so `hydrate()` finds no token and the app shows the login screen once. The pull request offers a one-time migration that reads the old MMKV value, writes it to the keychain and deletes it, if the team prefers no sign-in prompt.

### Does this need a new native build?

Yes. `expo-secure-store` is a native module, so a development client or EAS build is needed to run it on a device; `expo-doctor` in the run confirms the version matches the SDK. The Jest suite runs against a mock and needs no build.

## More React Native jobs

- [Investigate a Crash Spike in a React Native App](https://getrush.ai/run/investigate-crash-spike/react-native/)
- [Review Pull Requests on a React Native App](https://getrush.ai/run/review-pull-requests/react-native/)
- [Upgrade the Expo SDK in a React Native App](https://getrush.ai/run/upgrade-expo-sdk/react-native/)

## More jobs like this: Turn a Linear label into a PR

- [Add Pod and Container Security Contexts to a Helm Chart](https://getrush.ai/run/add-security-context/helm/)
- [Add Security Headers and a CSP to an Eleventy Site](https://getrush.ai/run/add-security-headers/eleventy/)
- [Add Security Headers to a Symfony App](https://getrush.ai/run/add-security-headers/symfony/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
