# Authentication

The CLI uses a device-code style browser approval flow. The terminal asks the API to create a device authorization, then a signed-in browser approves that user code.

## Login

```bash
selltraces login
```

For SSH, CI-like shells, or terminals where opening a browser is not useful:

```bash
selltraces login --no-browser
```

## Device login flow

```mermaid
sequenceDiagram
  participant CLI
  participant API as SellTraces API
  participant Browser
  participant Auth as Better Auth

  CLI->>API: POST /api/cli/device/start
  API->>Auth: create device code for selltraces-cli
  API-->>CLI: userCode, deviceCode, verification links
  Browser->>API: GET /cli/activate?user_code=...
  API->>Auth: verify and approve signed-in user
  CLI->>API: GET /api/cli/device/poll?deviceCode=...
  API->>Auth: exchange device code for bearer token
  API-->>CLI: token, userId
```

The API only accepts the fixed `selltraces-cli` client id for CLI device login. Polling returns:

| State | API response |
| --- | --- |
| Pending approval | `202` with `{ ok: true, status: "pending" }` |
| Approved | `200` with `{ ok: true, status: "approved", token, userId }` |
| Expired | `410` |
| Already consumed | `409` |
| Invalid code | `400` |

## Machine token storage

After approval, the CLI writes the token to:

```text
~/.selltraces/credentials.json
```

API requests use that token as a bearer credential:

```http
Authorization: Bearer <token>
```

The web app also supports authenticated browser sessions for the same user-scoped API routes. Routes that can be used by the CLI call the shared `getRequestUserId()` path, which accepts legacy machine bearer tokens and Better Auth bearer sessions before falling back to browser session cookies.

## Logout

```bash
selltraces logout
```

Logout removes this machine's saved credentials. It does not delete the saved upload policy or local trace ledger, so returning users keep source approvals, blocked terms, and already-synced history.

## Local development auth test

Run the web app with real Better Auth enabled:

```bash
# apps/web/.env.local
DEV_AUTH_BYPASS=0
BETTER_AUTH_URL=http://localhost:3000
APP_URL=http://localhost:3000
AUTH_REQUIRE_EMAIL_VERIFICATION=0
```

Then link the source-backed local CLI and run it with an isolated home:

```bash
pnpm cli:link-local
export SELLTRACES_TEST_HOME="$(mktemp -d)"

HOME="$SELLTRACES_TEST_HOME" selltraces login --no-browser
```

The local `selltraces` shim defaults to `http://localhost:3000`. The published
CLI defaults to `https://app.rebates.ai`; use `--api` or `SELLTRACES_API_URL`
only when intentionally targeting another origin.
