# Bagsy — Family Shopping List App A native iOS shopping list app built with **Swift/SwiftUI** and a **TypeScript/Node.js** backend. ## What is Bagsy? Bagsy lets families share and sync shopping lists in real time. Key features: - Passkey authentication (Face ID / Touch ID) + email/password fallback - Household management — invite family members via 6-character join codes - Shopping lists organized by store aisle - Real-time sync across devices via WebSocket - AI product suggestions (OpenRouter) - Barcode scanning support - Full dark mode ## Architecture ``` / ├── apps/ │ ├── api/ # Next.js 16 backend (API routes only, port 3001) │ │ ├── app/api/ # Route handlers (auth, households, lists, items, stores, ai) │ │ ├── db/ # Drizzle ORM schema + PostgreSQL connection │ │ ├── lib/ # auth.ts, websocket.ts, middleware │ │ └── server.ts # Custom HTTP server (WebSocket + local IP display) │ │ │ └── ios/Bagsy/ # Native Swift/SwiftUI iOS app │ └── Bagsy/ShopList/ │ ├── App/ # AppState & lifecycle │ ├── Core/ # API client, Keychain, Cache, WebSocket, Theme │ ├── Features/ # Auth, Lists, Stores screens │ └── Models/ # Data models │ └── packages/ └── types/ # Shared TypeScript types ``` **Stack** | Layer | Technology | |------------|----------------------------------------------| | iOS app | Swift 5.9+, SwiftUI, Combine | | Backend | Node.js 20+, Next.js 16, TypeScript | | Database | PostgreSQL + Drizzle ORM | | Auth | Better Auth (passkey, email/password, magic link, organizations) | | Real-time | WebSocket (ws) | | AI | Vercel AI SDK + OpenRouter | | Monorepo | Turborepo + bun workspaces | ## Running Locally ### Prerequisites - macOS 12+ with Xcode 14+ - [bun](https://bun.sh) 1.2+ - PostgreSQL 13+ ### 1. Install dependencies ```bash bun install ``` ### 2. Configure the backend ```bash cp apps/api/.env.example apps/api/.env.local ``` Edit `apps/api/.env.local`: ```bash BETTER_AUTH_SECRET=$(openssl rand -base64 32) BETTER_AUTH_URL=http://localhost:3001 DATABASE_URL=postgresql://user:password@localhost:5432/bagsy OPENROUTER_API_KEY=sk-or-... # optional, for AI suggestions NODE_ENV=development PORT=3001 ``` ### 3. Apply database migrations ```bash cd apps/api bun run db:push ``` ### 4. Start the backend ```bash cd apps/api bun run dev # Prints local network IP for iOS simulator/device ``` ### 5. Open the iOS app ```bash cd apps/ios/Bagsy open Bagsy.xcodeproj # Press Cmd+R in Xcode to build and run ``` Update the server URL in `AppState.swift` to match the IP printed by the backend: ```swift let serverURL = URL(string: "http://192.168.x.x:3001")! ``` ## Deploying The backend is a standard Next.js app with a custom Node server. Deploy to any provider that supports Node.js. ### Vercel ```bash cd apps/api npx vercel deploy ``` Note: Vercel serverless functions don't support WebSocket. Use Railway or Render for full WebSocket support. ### Railway / Render 1. Connect your repository. 2. Set the root directory to `apps/api`. 3. Set build command: `bun run build` 4. Set start command: `bun run start` 5. Add environment variables (see below). ### Required environment variables (production) ```bash BETTER_AUTH_SECRET= BETTER_AUTH_URL=https://api.yourdomain.com DATABASE_URL=postgresql://... OPENROUTER_API_KEY=... # optional NODE_ENV=production ``` ### iOS release build ```bash cd apps/ios/Bagsy xcodebuild archive -scheme Bagsy -configuration Release # Upload via Xcode Organizer or Transporter.app ``` ## Auth Setup (Better Auth + Passkeys) Auth is configured in `apps/api/lib/auth.ts` using [Better Auth](https://www.better-auth.com). **Plugins enabled:** - `organization` — maps to Bagsy "households" (family groups) - `passkey` — WebAuthn / Face ID / Touch ID - `magicLink` — email magic links (logs to console in dev; wire up a mail provider for prod) **Key routes (all under `/api/auth/[...all]`):** | Endpoint | Description | |---|---| | `POST /api/auth/sign-up/email` | Register with email + password | | `POST /api/auth/sign-in/email` | Sign in with email + password | | `POST /api/auth/passkey/generate-registration-options` | Begin passkey registration | | `POST /api/auth/passkey/verify-registration` | Complete passkey registration | | `POST /api/auth/passkey/generate-authentication-options` | Begin passkey sign-in | | `POST /api/auth/passkey/verify-authentication` | Complete passkey sign-in | **Requirements:** - PostgreSQL is required (passkey and organization tables need it) - `BETTER_AUTH_SECRET` must be set and stable (changing it invalidates all sessions) - `BETTER_AUTH_URL` must match the actual origin the app connects to (used as WebAuthn `rpID`) **iOS client** uses `@better-auth/expo` for cookie/session management compatible with native HTTP clients. ## Common Issues **iOS can't connect to API** - Confirm backend is running: `ps aux | grep node` - Make sure the IP in `AppState.swift` matches what `bun run dev` printed - Device and Mac must be on the same Wi-Fi network **Passkey endpoints return empty responses** - Ensure `BETTER_AUTH_URL` is set correctly in `.env.local` - The passkey plugin reads `baseURL` to derive the WebAuthn `rpID` - Restart the backend after changing env vars **Database connection errors** - Confirm `DATABASE_URL` is set and PostgreSQL is running - Re-run `bun run db:push` after schema changes