# Deploying Cairn backend v2 (no login) to cPanel

This replaces the version you already uploaded. It has no Apple/Google/email
sign-in anywhere — identity is just a token issued per device, no passwords,
no JWT, no mail service. Your database is still empty, so there's nothing
to migrate away from; you're just importing a cleaner schema.

## 1. Replace the files

In cPanel File Manager, inside `cairn.kopixxsoftware.com/`, delete everything
except `.well-known` (leave that alone). Then upload and extract this zip
into that same folder, so `app.js` sits directly inside
`cairn.kopixxsoftware.com/app.js` (not nested one level deeper — check this,
it's the single most common cPanel upload mistake).

You can also delete the stray `2BACKENDnodejsapi.zip` you had sitting in
that folder — it's not doing anything, just clutter.

## 2. Run npm install

cPanel → Setup Node.js App → click your app → **Run NPM Install**.

If that button doesn't visibly finish, use Terminal instead: cPanel → Terminal,
paste the "enter virtual environment" command shown at the top of your app's
Node.js Selector page, then run `npm install` yourself and watch it complete.

## 3. Create `.env`

Copy `.env.example` to `.env` in the same folder, then fill in:

- `DB_NAME`, `DB_USER`, `DB_PASSWORD` — from cPanel → MySQL Databases (your
  database is `kopixxso_cairn`; create a MySQL user for it if you haven't,
  with a real password, and grant it *all privileges* on that database).
- `ENCRYPTION_KEY` and `APP_SECRET` — generate each with:
  ```
  node -e "console.log(require('crypto').randomBytes(32).toString('base64'))"
  ```
  Run it twice, once per value — they must be different from each other.
- `REVENUECAT_WEBHOOK_SECRET` — from the RevenueCat dashboard once you set
  that up (Project Settings → Integrations → Webhooks). You can leave this
  blank for now and fill it in later — the app will run, it'll just refuse
  webhook calls until it's set. `preflight.js` will remind you.
- `ANTHROPIC_API_KEY` is optional — leave blank and the AI Coach still
  works, just with a simpler fallback reply instead of a real conversation.

## 4. Import the schema

phpMyAdmin → select `kopixxso_cairn` → **Import** tab → choose
`schema.sql` from this backend folder → Go. That's the only file to import —
there's no separate migrations step needed for a fresh database.

(Alternatively, from Terminal inside the app's virtual environment:
`npm run migrate`.)

## 5. Seed reference data

From Terminal, inside the virtual environment:
```
npm run seed
```
This adds the emotion/trigger/mission catalog data the app needs.

## 6. Preflight check

Still in Terminal:
```
npm run preflight
```
Every line should say `PASS` (an `ANTHROPIC_API_KEY` warning is fine to
ignore for now). Fix anything that says `FAIL` before moving on.

## 7. Restart the app

cPanel → Setup Node.js App → your app → **Restart**.

## 8. Verify it's alive

From your own computer's terminal, or a browser:
```
curl https://cairn.kopixxsoftware.com/v1/health
```
Should return `{"ok":true,"db":true,"env":"production"}`.

Then simulate what the app itself will do on first launch:
```
curl -X POST https://cairn.kopixxsoftware.com/v1/devices/register \
  -H "Content-Type: application/json" \
  -d '{"installId":"manual-test-1234567890","platform":"ios"}'
```
Should return a `deviceToken`. Save it, then check entitlement:
```
curl https://cairn.kopixxsoftware.com/v1/me/entitlement \
  -H "Authorization: Bearer <the deviceToken from above>"
```
Should return `{"isPremium":false,"source":"none",...}` — correct, since
nobody's purchased or been granted anything yet.

## 9. Give yourself premium for testing

```
node scripts/grant-premium.js --install-id=manual-test-1234567890 --reason="my own testing"
```
Re-run the entitlement curl above — it should now say `"isPremium":true`.

## About Apple App Review

There's no login screen for a reviewer to enter credentials into. That's
fine and normal for a subscription app with no accounts — in App Store
Connect's App Review Information, leave "Sign-in required" unchecked, and
add a note in the Review Notes field explaining that the app has no
accounts and Apple's reviewer should complete the subscription purchase
using their sandbox tester Apple ID to access premium features, the same
way they'd test any subscription-only app.
