# NearMe Admin Web App — PRD

## Original Problem Statement
"Create a web app to manage this mobile app" + iterations: manage all APIs, categories CRUD, dark mode, CSV import, weather forecast, referral, multi-user (deferred), and consolidate mobile app into this repo.

## Delivered (through Feb 2026)
1. **Auth (v2)** — Unified JWT-like sessions with **email/password** + **Emergent Google Auth**. First signup becomes `owner`, subsequent signups are `user`. Roles: owner / editor / viewer / user. Legacy `X-Admin-Token: admin123` still works for backwards compat on non-user-management endpoints. Rotatable via `/settings` (legacy) or via user-management UI.
2. **Users & Roles** — `/users` admin page: list, promote/demote via role dropdown (owner only), delete other users (owner only), search. Signed-in user badge in sidebar.
2. **Dashboard** — live API/Mongo/Google/favorites/categories status.
3. **Ad Configuration** — global toggles + per-placement banner + interstitial CRUD.
4. **Video Ad Rules** — CRUD with category/city targeting, priority, skip-after.
5. **Ad-config history + one-click rollback** (`/ads/history`).
6. **Analytics** — public `/api/analytics/track` for mobile, admin `/analytics` page with KPIs, per-rule CTR table, favorites-by-category chart.
7. **Favorites** — search + delete + open-in-Google-Maps.
8. **Categories** — full CRUD + **CSV import** (merge/upsert/replace modes) + reset.
9. **API Keys / Secrets vault** — Google Maps, Directions, Weather, AdMob Android, AdMob iOS, Backend URL — all editable in-UI, DB overrides `.env`.
10. **Places API Tester** — live nearby-search via backend proxy.
11. **Places API details** — `GET /api/places/{id}?lat=&lng=` added for mobile detail screen.
12. **Weather** — public `/api/weather` + `/api/weather/forecast` (OpenWeatherMap proxy).
13. **Mobile Connection page** — shows `EXPO_PUBLIC_BACKEND_URL`, one-click connectivity checks.
14. **Dark Mode** — Light / System / Dark toggle in sidebar, persists to localStorage.
15. **Mobile app consolidated** at `/app/mobile/` — cloned from
    https://github.com/dukejulian30/mobile-app, package renamed to `com.nearme.app`,
    `EXPO_PUBLIC_BACKEND_URL` pointed at this preview, weather + analytics helpers
    added to `src/api.ts`.
16. **Referral / points system** (Feb 2026) — backend: `/api/referrals/me`,
    `/api/referrals/apply-code`, `/api/referrals/summary`; web admin dashboard;
    on-signup 50-pt bonuses to both parties.
17. **Mobile Auth + Referrals sync (Feb 2026)** — mobile app now consumes
    `/api/auth/{register,login-email,session,me,logout}` and `/api/referrals/*`.
    New screens: `app/auth/login.tsx`, `app/auth/register.tsx`,
    `app/auth/callback.tsx`, `app/refer.tsx`, `app/profile.tsx`. New
    `AuthProvider` in `src/auth/AuthContext.tsx` with SecureStore token
    persistence + Bearer-header attach. Emergent Google Auth via
    `expo-web-browser` + deep link (`nearme://auth/callback`). Home-screen
    drawer shows compact **Weather widget** and a Sign-in / Profile card.
18. **NearMe branding + hardening (Feb 2026)** — new logo (map-pin over orange
    grid), splash + icon regenerated at 1024×1024, web favicon + title updated,
    Google Maps key configured in DB secrets. `/api/places/nearby` now returns
    a **503 with actionable text** when Places API (New) is disabled at Google
    Cloud level; mobile right drawer shows the message and a Retry button.
    **Rate-limiting** on `/auth/login-email` (10/min/IP) and `/auth/register`
    (5/min/IP) — in-memory sliding window, 429 with `Retry-After`.
19. **First-party Google OAuth (Feb 2026)** — new `GET /api/auth/google/start`
    + `GET /api/auth/google/callback` implement the standard Google OAuth 2.0
    Authorization-Code flow. Credentials are configured entirely from the
    **web admin `/secrets` page** (new fields:
    `google_oauth_client_id`, `google_oauth_client_secret`,
    `google_oauth_web_redirect_uri`) — no code or `.env` changes needed.
    `GET /api/auth/config` returns `{ google_provider: "own" | "emergent" }`;
    both the web `Login` and mobile Login screens auto-detect which flow to
    use, so the Emergent-hosted redirect page is bypassed the moment the
    admin pastes valid client credentials.
20. **Auto-logout bug fix + login UX cleanup (Feb 2026)** —
    - `Layout.jsx` now only calls `/auth/me` when a JWT user token exists
      (legacy X-Admin-Token admins used to trigger a 401 → auto-bounce loop).
    - Response interceptor now bounces admins to `/admin/login` (not `/login`).
    - **Legacy admin password tab removed** from the login UI (JWT accounts
      only). Backend `/api/auth/login` route left intact for scripts/tests.
    - **"Continue with Google" button is hidden entirely when
      `own_google_enabled` is false** on both the web `Login.jsx` and mobile
      `app/auth/login.tsx` — so users no longer see any Emergent-branded
      auth page in the background.
21. **Mobile canonical API URL builder + AI Concierge relocation (Feb 2026)** —
    - Completed the `apiUrl(path)` refactor in `/app/mobile/frontend/src/api.ts`:
      every one of the 20 `fetch()` sites now goes through the helper, which
      strips trailing slashes on the base and guarantees exactly one `/api`
      prefix. Zero `${BACKEND_URL}` template references remain — the previous
      partial refactor was silently broken (undefined identifier) and was the
      root cause of the "no categories on device" bug.
    - Runtime backend override (Profile → long-press URL) still works and is
      restored from AsyncStorage inside `_layout.tsx` before the AuthProvider
      calls `/auth/me`.
    - **AI Local Concierge moved to a prominent floating card on the main map
      screen** (`styles.aiCard` in `app/index.tsx`, positioned just below the
      top bar with brand-orange border, sparkles badge, pill-shaped input, and
      brand submit button). Loading state is inline (spinner inside submit
      button). The left drawer now only contains a plain category filter.
      Results still open in the existing right drawer via `fetchAiSearch`.
    - Verified by testing subagent (iteration_7.json): 10/10 backend regression
      checks passed on both `https://nearmeon.com` and the preview backend;
      code-level review confirmed no residual `BACKEND_URL` refs, the AI card
      is on the map surface, and the drawer no longer duplicates the AI input.

## Tech
- Backend: FastAPI + Motor + httpx.
- Frontend: React 19 + Shadcn + Tailwind (class-based dark mode), Plus Jakarta Sans, Lucide, Sonner, recharts.
- Mobile: Expo (Router v6), TypeScript, react-native-maps.

## Deferred (each needs its own iteration)
- **Referral / points system** (next up — depends on the user model now delivered).
- Splitting web app into public marketing site + separate admin subdomain.
- Rate-limiting on `/auth/login-email` and `/auth/register` (brute-force protection).
- Split `server.py` (1.3k lines) into feature-scoped routers.
- Refresh-token / sliding-window sessions (currently 7-day fixed).

## Next Action Items
- **User action required — enable Places API (New)** on Google Cloud project
  `1056354034213` at https://console.cloud.google.com/apis/library/places.googleapis.com
  (currently returns `API_KEY_SERVICE_BLOCKED`; the mobile right-drawer now
  shows the actionable message + Retry button).
- **User action required — provide an OpenWeatherMap API key** so the mobile
  weather widget lights up (`/secrets` → `weather_api_key`).
- **User action required — provide a Google OAuth Client ID / Secret** so we
  can replace the Emergent-hosted auth page with a first-party Google prompt.
- Split `server.py` (1.4k lines) into feature-scoped routers (auth, referrals,
  places, secrets, ad-config, analytics).
- Upload the full ~4000-row Google categories CSV via the `/categories` Import
  CSV dialog when ready.
