The auth endpoint was returning 404 because the route handler wasn't properly using better-auth's Next.js integration. Use toNextJsHandler() to properly export the handlers. |
||
|---|---|---|
| .deployment | ||
| .github/workflows | ||
| apps | ||
| docs | ||
| packages/types | ||
| scripts | ||
| .eslintrc.json | ||
| .gitignore | ||
| .prettierrc.json | ||
| AGENTS.md | ||
| app.json | ||
| bun.lock | ||
| eas.json | ||
| FILE_STRUCTURE.txt | ||
| FINAL_STATUS.txt | ||
| package.json | ||
| PROJECT_STATUS.md | ||
| README.md | ||
| sst.config.ts | ||
| STATUS.txt | ||
| tsconfig.json | ||
| turbo.json | ||
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 1.2+
- PostgreSQL 13+
1. Install dependencies
bun install
2. Configure the backend
cp apps/api/.env.example apps/api/.env.local
Edit apps/api/.env.local:
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
cd apps/api
bun run db:push
4. Start the backend
cd apps/api
bun run dev
# Prints local network IP for iOS simulator/device
5. Open the iOS app
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:
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
cd apps/api
npx vercel deploy
Note: Vercel serverless functions don't support WebSocket. Use Railway or Render for full WebSocket support.
Railway / Render
- Connect your repository.
- Set the root directory to
apps/api. - Set build command:
bun run build - Set start command:
bun run start - Add environment variables (see below).
Required environment variables (production)
BETTER_AUTH_SECRET=<openssl rand -base64 32>
BETTER_AUTH_URL=https://api.yourdomain.com
DATABASE_URL=postgresql://...
OPENROUTER_API_KEY=... # optional
NODE_ENV=production
iOS release build
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.
Plugins enabled:
organization— maps to Bagsy "households" (family groups)passkey— WebAuthn / Face ID / Touch IDmagicLink— 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_SECRETmust be set and stable (changing it invalidates all sessions)BETTER_AUTH_URLmust match the actual origin the app connects to (used as WebAuthnrpID)
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.swiftmatches whatbun run devprinted - Device and Mac must be on the same Wi-Fi network
Passkey endpoints return empty responses
- Ensure
BETTER_AUTH_URLis set correctly in.env.local - The passkey plugin reads
baseURLto derive the WebAuthnrpID - Restart the backend after changing env vars
Database connection errors
- Confirm
DATABASE_URLis set and PostgreSQL is running - Re-run
bun run db:pushafter schema changes