feat(auth): implement session-based authentication and API token management
Server CI / Lint (push) Successful in 19s
Server CI / Build & Push Docker Image (push) Failing after 44s

- 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:
Gabriel20xx committed 2026-08-01 12:51:02 +02:00
1 parent 240e1fda4a
commit b150d835a1
18 files changed
+1022 -982

No files matched your search

+18 -13
View File
@@ -1,9 +1,11 @@
import express from 'express';
import cors from 'cors';
import cookieParser from 'cookie-parser';
import dotenv from 'dotenv';
import { initializeDatabase } from './db';
import { authenticatePassword } from './middleware/auth';
import { requireApiToken } from './middleware/auth';
import { deviceIdMiddleware } from './middleware/device';
import authRoutes from './routes/authRoutes';
import listRoutes from './routes/listRoutes';
import fieldRoutes from './routes/fieldRoutes';
import itemRoutes from './routes/itemRoutes';
@@ -51,27 +53,35 @@ async function initializeDatabaseWithRetry() {
}
// Middleware
app.use(cors());
app.use(cors({ credentials: true, origin: true }));
app.use(express.json());
app.use(cookieParser());
// Health check (no auth required)
app.get('/health', (req, res) => {
res.json({ status: 'ok', timestamp: Date.now() });
});
// Apply authentication middleware to all API routes
app.use('/api', authenticatePassword);
// Auth bootstrap routes (status/setup/login/logout/credentials/token) - must stay
// reachable without a token or session, mounted before the API-token gate below.
app.use('/api', authRoutes);
// Sync/collaboration API: gated by a long-lived API token shared by every collaborating
// device (see middleware/auth.ts) - this replaces the old optional SERVER_PASSWORD scheme,
// which silently disabled auth entirely when unset.
app.use('/api', requireApiToken);
// Attach device id for downstream routes
app.use('/api', deviceIdMiddleware);
// Routes (protected by auth)
// Routes (protected by the API token above)
app.use('/api/lists', listRoutes);
app.use('/api/fields', fieldRoutes);
app.use('/api/items', itemRoutes);
app.use('/api', syncRoutes);
app.use('/api', notificationRoutes);
// Web UI (no auth required, must be last to not interfere with API routes)
// Web UI (static SPA shell is public; its /web/info and /web/data JSON endpoints require
// the admin session - see webRoutes.ts. Mounted last to not interfere with API routes.)
app.use('/', webRoutes);
// Initialize database and start server
@@ -92,13 +102,8 @@ app.use('/', webRoutes);
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
const dbClient = (process.env.DB_CLIENT || process.env.DB_TYPE || 'sqlite').toLowerCase();
if (dbClient === 'postgres' || dbClient === 'postgresql') {
const source = process.env.DATABASE_URL ? 'DATABASE_URL' : 'PGHOST/PGDATABASE';
console.log(`Database: PostgreSQL (${source})`);
} else {
console.log(`Database: ${process.env.DB_PATH || './data/collabtable.db'}`);
}
const source = process.env.DATABASE_URL ? 'DATABASE_URL' : 'PGHOST/PGDATABASE';
console.log(`Database: PostgreSQL (${source})`);
});
} catch (error) {
console.error('Database initialization error:', error);