# Backend Development Guide This guide covers server-side features including authentication, database, tRPC API, and integrations. **Only read this if your app needs these capabilities.** --- ## When Do You Need Backend? | Scenario | Backend Needed? | User Auth Required? | Solution | |----------|-----------------|---------------------|----------| | Data stays on device only | No | No | Use `AsyncStorage` | | Data syncs across devices | Yes | Yes | Database + tRPC | | User accounts / login | Yes | Yes | Manus OAuth | | AI-powered features | Yes | **Optional** | LLM Integration | | User uploads files | Yes | **Optional** | S3 Storage | | Server-side validation | Yes | **Optional** | tRPC procedures | > **Note:** Backend ≠ User Auth. You can run a backend with LLM/Storage/ImageGen capabilities without requiring user login — just use `publicProcedure` instead of `protectedProcedure`. User auth is only mandatory when you need to identify users or sync user-specific data. --- ## File Structure ``` server/ db.ts ← Query helpers (add database functions here) routers.ts ← tRPC procedures (add API routes here) storage.ts ← S3 storage helpers (can extend) _core/ ← Framework-level code (don't modify) drizzle/ schema.ts ← Database tables & types (add your tables here) relations.ts ← Table relationships migrations/ ← Auto-generated migrations shared/ types.ts ← Shared TypeScript types const.ts ← Shared constants _core/ ← Framework-level code (don't modify) lib/ trpc.ts ← tRPC client (can customize headers) _core/ ← Framework-level code (don't modify) hooks/ use-auth.ts ← Auth state hook (don't modify) tests/ *.test.ts ← Add your tests here ``` Only touch the files with "←" markers. Anything under `_core/` directories is framework-level—avoid editing unless you are extending the infrastructure. --- ## Authentication ### Overview The template uses **Manus OAuth** for user authentication. It works differently on native and web: | Platform | Auth Method | Token Storage | |----------|-------------|---------------| | iOS/Android | Bearer token | expo-secure-store | | Web | HTTP-only cookie | Browser cookie | ### Using the Auth Hook ```tsx import { useAuth } from "@/hooks/use-auth"; function MyScreen() { const { user, isAuthenticated, loading, logout } = useAuth(); if (loading) return ; if (!isAuthenticated) { return ; } return ( Welcome, {user.name}