Prompt
How do I set up authentication, database, and file storage for a mobile app backend?
Latest observation
A good way to think about a mobile app backend is as 3 separate concerns:
- Authentication: who the user is
- Database: app data and relationships
- File storage: images, videos, documents, etc.
Below is a practical setup that works well for most mobile apps.
1) Authentication
Best practice
Use a proven identity provider instead of building auth from scratch unless you have a strong reason.
Common options:
- Auth0
- Firebase Authentication
- AWS Cognito
- Supabase Auth
- Your own backend with JWT + refresh tokens
Typical mobile auth flow
- User signs up or logs in
- Backend/identity provider returns:
- access token: short-lived
- refresh token: long-lived
- Mobile app stores tokens securely
- App sends access token in API requests:
Authorization: Bearer <token>
- When access token expires, app uses refresh token to get a new one
Mobile token storage
- iOS: Keychain
- Android: EncryptedSharedPreferences or Keystore-backed storage
- Cross-platform:
- React Native:
react-native-keychain,expo-secure-store - Flutter:
flutter_secure_storage
- React Native:
Security tips
- Never store tokens in plain local storage
- Use HTTPS everywhere
- Make access tokens short-lived
- Support token revocation/logout
- If using your own auth, hash passwords with Argon2 or bcrypt
2) Database
Choose the right database
For most app backends, start with a relational database:
- PostgreSQL is usually the best default
Why:
- Good for user data, permissions, relationships
- Strong consistency
- Easy to query and maintain
- Works well with backend frameworks
Use NoSQL only if your data is highly document-based or you have a specific scaling/use-case need.
Typical schema example
For a simple mobile app, you might have:
usersprofilessessionsorrefresh_tokenspostscommentsnotificationsuploadsormedia_files
Example users table:
id UUID PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
password_hash TEXT,
created_at TIMESTAMP NOT NULL DEFAULT now()
Access pattern
Your mobile app should never connect directly to the database. Instead:
Mobile app → API server → Database
This keeps credentials and logic on the server.
ORM / query tools
Useful options:
- Node.js: Prisma, TypeORM, Sequelize
- Python: SQLAlchemy, Django ORM
- Go: GORM, sqlc
- Java/Kotlin: Hibernate, Exposed
- .NET: Entity Framework Core
3) File storage
Don’t store files in your app server filesystem
For production, use object storage:
- Amazon S3
- Google Cloud Storage
- Azure Blob Storage
- Cloudflare R2
- Supabase Storage
Recommended pattern
- Mobile app asks backend for an upload URL
- Backend generates a pre-signed URL
- App uploads file directly to storage
- Backend saves the file metadata in the database
This is better because:
- Your API server doesn’t handle large uploads
- Storage scales independently
- More secure and cheaper
What to store in the database
Store metadata, not the file itself:
- file URL or object key
- user_id
- mime type
- size
- created_at
- status
Example:
id UUID PRIMARY KEY,
user_id UUID NOT NULL,
object_key TEXT NOT NULL,
mime_type TEXT NOT NULL,
size_bytes BIGINT,
created_at TIMESTAMP DEFAULT now()
File upload flow
- App requests:
POST /uploads/presign - Backend checks auth and returns:
- upload URL
- object key
- App uploads file directly to storage
- App notifies backend:
POST /uploads/complete - Backend records file metadata
Security tips
- Restrict file types and sizes
- Scan uploaded files if needed
- Use private buckets for sensitive files
- Serve private files through signed URLs or authenticated proxy endpoints
Recommended architecture
A simple and solid backend setup:
- Mobile app
- API backend: Node.js / Python / Go / Java
- Auth provider: Auth0/Firebase/Cognito or custom JWT auth
- Database: PostgreSQL
- File storage: S3 or equivalent
- Cache/queue: Redis if needed later
Example stack choices
Option A: Fast to build
- Firebase Auth
- Firestore or PostgreSQL
- Firebase Storage
- Cloud Functions / Cloud Run
Option B: More control, still manageable
- Custom backend with NestJS / Django / FastAPI / Express
- PostgreSQL
- S3
- JWT auth
Option C: Supabase
- Supabase Auth
- Supabase Postgres
- Supabase Storage
- Great for small/medium apps
Practical implementation order
- Set up your backend API
- Add authentication
- Create database schema
- Add file storage integration
- Add authorization rules
- Add logging, monitoring, backups
- Add rate limiting and security hardening
Common mistakes to avoid
- Putting secrets in the mobile app
- Connecting the app directly to the DB
- Storing files on the backend server disk
- Using long-lived access tokens
- Skipping authorization checks
- Not validating file uploads
- Not using HTTPS
- Rolling your own auth too early
If you want, I can also give you:
- a sample architecture diagram
- a step-by-step setup for Node.js / Django / FastAPI
- or a backend template with auth + Postgres + S3
Brands