feat(auth): implement session-based authentication and API token management
- Introduced a new authentication system using session cookies for the web dashboard and API tokens for sync clients. - Replaced the old SERVER_PASSWORD scheme with a more secure API token mechanism. - Added routes for user setup, login, logout, and credential updates. - Implemented middleware to require session and API token for protected routes. - Updated web UI to handle authentication states and provide user feedback. - Enhanced error handling in API requests and responses. - Updated README to reflect new authentication flow and database requirements.
This commit is contained in:
1 parent
240e1fda4a
commit
b150d835a1
18 files changed
+1022
-982
No files matched your search
+32
-37
@@ -1,19 +1,21 @@
|
||||
# CollabTable Server
|
||||
|
||||
A collaborative table management server built with Node.js, Express, TypeScript, and a pluggable database (SQLite or PostgreSQL).
|
||||
A collaborative table management server built with Node.js, Express, TypeScript, and PostgreSQL.
|
||||
|
||||
## Features
|
||||
|
||||
- RESTful API for managing lists, fields, items, and values
|
||||
- Real-time synchronization via WebSocket (primary) with HTTP fallback
|
||||
- SQLite (better-sqlite3) or PostgreSQL (pg) database backends
|
||||
- PostgreSQL (pg) database backend
|
||||
- Single-admin-account web dashboard login (session cookie, bcrypt-hashed password) plus a
|
||||
long-lived API token for sync clients/devices, generated from the dashboard
|
||||
- Dockerized deployment with persistent storage
|
||||
- TypeScript for type safety
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Docker and Docker Compose
|
||||
- Or Node.js 20+ (for local development)
|
||||
- Or Node.js 20+ and a PostgreSQL instance (for local development)
|
||||
|
||||
## Quick Start with Docker
|
||||
|
||||
@@ -28,14 +30,13 @@ A collaborative table management server built with Node.js, Express, TypeScript,
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
4. Start the services:
|
||||
4. Start the services (this also starts a bundled PostgreSQL container):
|
||||
```bash
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
The server will be available at `http://localhost:3000`
|
||||
|
||||
**Important:** The default backend is SQLite with a Docker volume `sqlite_data` for persistence. You can also switch to PostgreSQL by setting environment variables.
|
||||
The server will be available at `http://localhost:3000`. Open it in a browser to create the
|
||||
admin account, then generate an API token from the dashboard for your sync clients/devices.
|
||||
|
||||
## Local Development
|
||||
|
||||
@@ -49,21 +50,18 @@ The server will be available at `http://localhost:3000`
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
3. Choose your database in `.env` (SQLite by default):
|
||||
3. Point it at a PostgreSQL instance in `.env`:
|
||||
```
|
||||
# Backend: sqlite | postgres (default sqlite)
|
||||
DB_CLIENT=sqlite
|
||||
|
||||
# SQLite
|
||||
DB_PATH=./data/collabtable.db
|
||||
|
||||
# OR PostgreSQL
|
||||
# DATABASE_URL=postgres://user:password@localhost:5432/collabtable
|
||||
DATABASE_URL=postgres://user:password@localhost:5432/collabtable
|
||||
# OR, as discrete variables:
|
||||
# PGHOST=localhost
|
||||
# PGPORT=5432
|
||||
# PGUSER=postgres
|
||||
# PGPASSWORD=yourpassword
|
||||
# PGDATABASE=collabtable
|
||||
|
||||
# Set to "true" once served over HTTPS
|
||||
COOKIE_SECURE=false
|
||||
```
|
||||
|
||||
4. Start the development server:
|
||||
@@ -102,7 +100,7 @@ The server will be available at `http://localhost:3000`
|
||||
- `{"type":"ping"}` → `{"type":"pong"}`
|
||||
- `{"type":"sync", "id":"<uuid>", "payload": { ...SyncRequest }}` → `{"type":"syncResponse", "id":"<uuid>", "payload": { ...SyncResponse }}`
|
||||
|
||||
Authentication: If `SERVER_PASSWORD` is set, include header `Authorization: Bearer <password>` on HTTP and WebSocket requests.
|
||||
Authentication: include header `Authorization: Bearer <api-token>` on sync/collaboration API requests, using the API token generated from the web dashboard's Settings page (see "Authentication" below).
|
||||
|
||||
### Health Check
|
||||
- `GET /health` - Check server health
|
||||
@@ -225,34 +223,31 @@ docker-compose up -d --build
|
||||
|
||||
- `PORT` - Server port (default: 3000)
|
||||
- `NODE_ENV` - Environment (development/production)
|
||||
- `SERVER_PASSWORD` - Optional shared password for API and WebSocket (recommended)
|
||||
- `DB_CLIENT` - `sqlite` (default) or `postgres`
|
||||
- `DB_PATH` - Path to SQLite database file (default: ./data/collabtable.db for local, /data/collabtable.db in Docker)
|
||||
- `COOKIE_SECURE` - Set to `true` to mark the admin session cookie `Secure` (requires HTTPS); default `false`
|
||||
- `DATABASE_URL` - PostgreSQL connection URL (optional alternative to PG* variables)
|
||||
- `PGHOST`, `PGPORT`, `PGUSER`, `PGPASSWORD`, `PGDATABASE` - PostgreSQL connection parameters
|
||||
- `DB_INIT_RETRY_INTERVAL_MS` - Delay between DB init retries in ms (default: `5000`)
|
||||
- `DB_INIT_RETRY_MAX_ATTEMPTS` - Max DB init retries; `0` retries forever (default: `0`)
|
||||
- `AUTH_LOG_THROTTLE_MS` - Minimum interval before repeating the same auth warning log (default: `30000`)
|
||||
- `SYNC_MAX_FUTURE_SKEW_MS` - Allowed future timestamp skew from clients during sync (default: `0`)
|
||||
|
||||
## Authentication
|
||||
|
||||
There is a single admin account for the web dashboard (username + password, bcrypt-hashed,
|
||||
session cookie - no accounts beyond the one admin). The first time you open the dashboard
|
||||
you're walked through creating it; there is no default/seeded account.
|
||||
|
||||
Sync clients and devices (the mobile app, or anything hitting the `/api/lists` /
|
||||
`/api/fields` / `/api/items` / `/api/sync` / `/api/notifications` endpoints) authenticate
|
||||
with a separate long-lived API token instead - generate or regenerate it from the
|
||||
dashboard's Settings page, then set `Authorization: Bearer <token>` on those requests. This
|
||||
replaces the old optional `SERVER_PASSWORD` scheme, which silently disabled auth entirely
|
||||
when left unset.
|
||||
|
||||
## Data Persistence
|
||||
|
||||
### SQLite
|
||||
The SQLite database file is stored in a Docker volume, ensuring data persists across container lifecycle events.
|
||||
|
||||
Backup:
|
||||
```bash
|
||||
docker cp collabtable-server:/data/collabtable.db ./backup.db
|
||||
```
|
||||
|
||||
Restore:
|
||||
```bash
|
||||
docker cp ./backup.db collabtable-server:/data/collabtable.db
|
||||
docker-compose restart
|
||||
```
|
||||
|
||||
### PostgreSQL
|
||||
Use your normal PostgreSQL backup/restore procedures (e.g., `pg_dump`, `pg_restore`). If using the optional Dockerized Postgres service, the data is stored in a named volume (see `docker-compose.yml`).
|
||||
Use your normal PostgreSQL backup/restore procedures (e.g., `pg_dump`, `pg_restore`). If
|
||||
using the bundled Dockerized Postgres service, the data is stored in a named volume (see
|
||||
`docker-compose.yml`).
|
||||
|
||||
## License
|
||||
|
||||
|
||||
Reference in new issue
Block a user