> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Feature — Auth

Sign-in, sign-up, sign-out, and the user profile screen. Session is persisted in MMKV. Backed by a mock API today; swap to a real backend by editing `api.ts` only.

## Files

```
src/features/auth/
  api.ts                              # mock signIn/signUp/signOut
  mappers.ts                          # SessionDto -> Session
  types.ts                            # User, Session, payloads
  queries.ts                          # useSignInMutation, useSignUpMutation, useSignOutMutation
  store.ts                            # zustand + MMKV (session)
  components/AuthForm.tsx             # email + password (+ name) form
  screens/SignInScreen.tsx
  screens/SignUpScreen.tsx
  screens/ProfileScreen.tsx
```

Routes:

- [app/(auth)/sign-in.tsx](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/app/\(auth\)/sign-in.tsx)
- [app/(auth)/sign-up.tsx](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/app/\(auth\)/sign-up.tsx)
- [app/(app)/(tabs)/profile.tsx](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/app/\(app\)/\(tabs\)/profile.tsx)

## Types at a glance

```mermaid
classDiagram
  class UserDto {
    +string id
    +string email
    +string display_name
    +string avatar_url
  }
  class User {
    +string id
    +string email
    +string displayName
    +string avatarUrl
  }
  class SessionDto {
    +UserDto user
    +string access_token
    +number expires_at
  }
  class Session {
    +User user
    +string accessToken
    +number expiresAt
  }
  SessionDto --> UserDto
  Session --> User
  SessionDto ..> Session: toSession()
  UserDto ..> User: toUser()
```

## Auth store

```ts
interface AuthState {
  session: Session | null;
  hydrated: boolean;
  setSession: (s: Session | null) => void;
  signOut: () => void;
  setHydrated: () => void;
}
```

- MMKV key: `auth`
- `partialize` whitelists only `session` — `hydrated` is local-only.
- `onRehydrateStorage` calls `setHydrated()` so screens can wait for storage if needed (currently MMKV is synchronous so this fires immediately).

## Mutations

```mermaid
flowchart LR
  ui[Screen] -->|mutate| useSignIn[useSignInMutation]
  useSignIn -->|signInRequest| api[api.ts]
  api -->|SessionDto| useSignIn
  useSignIn -->|toSession| session[Session]
  useSignIn -->|setSession| store[useAuthStore]
  store --> mmkv[(MMKV)]
  store --> rootLayout["app/_layout<br/>&lt;Stack.Protected&gt; re-evaluates"]
  rootLayout --> protected["/(app)/(tabs)"]
```

The screen never calls `router.replace`. It just runs the mutation; `setSession` updates the store, the root layout's `<Stack.Protected guard={hasCompleted && !!session}>` becomes the active block, and Expo Router mounts `(app)/(tabs)`. See [routing.md](/docs/routing.md) for the full guard model.

`useSignOutMutation` additionally calls `queryClient.clear()` so cached server data doesn't leak between users.

## Form

`AuthForm` is shared between sign-in and sign-up — the `mode` prop conditionally shows the `displayName` field. The parent owns the mutation; the form is a controlled trio of inputs + submit button.

```mermaid
flowchart LR
  parent[SignInScreen / SignUpScreen] -->|mode, loading, error, onSubmit| form[AuthForm]
  form -->|onSubmit values| parent
  parent -->|mutate| query[mutation hook]
```

## Mock API behaviour

`api.ts` simulates latency (400-800 ms) and rejects obviously bad inputs (no `@`, too-short password, empty display name). It returns a generated `SessionDto` so the rest of the stack can be exercised without a backend.

When wiring to a real API:

1. Replace bodies of `signInRequest` / `signUpRequest` / `signOutRequest` with `apiClient.post(...)` / `.delete(...)` calls.
2. Make sure the DTO shape from the server matches `SessionDto`; if not, adjust `types.ts` + `mappers.ts`.
3. Add `apiClient` `getAuthToken: () => useAuthStore.getState().session?.accessToken` so subsequent calls are authenticated.

## Profile screen

Reads `useAuthStore().session` for display and exposes:

- **Sign out** → `useSignOutMutation.mutate()`. Mutation's `onSuccess` flips `session` to `null`; root layout's `<Stack.Protected>` swaps to `(auth)/sign-in`. No `router` call.
- **Reset onboarding flow** → closes paywall, `useOnboardingStore.getState().reset()`, `useTutorialStore.getState().reset()`, and `signOut()`. The onboarding guard has the highest priority, so the root layout mounts `(onboarding)` for the full onboarding → paywall → tutorial sequence. No `router` call.

Tablet layout (`components/profile-tablet-layout.ts`, Figma "Profile - iPad A16"): the top bar (Go Premium + settings gear) sits 24 off the screen's top and right edges, and the Recent Song card's difficulty chip hugs its word with the label at 14. The phone keeps its own layout.

***

<small>Source: [`docs/features/auth.md`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/docs/features/auth.md)</small>
