Implement complete JWT-based authentication system with comprehensive security features:
Database:
- Migration 005: Add 4 new tables (refresh_tokens, password_reset_tokens, audit_log, entity_permissions)
- Enhanced users table with email verification, account status, lockout protection
Services:
- auth.service.js: Full authentication lifecycle (register, login, refresh, logout, password reset, email verification)
- audit.service.js: Comprehensive security event logging and tracking
Routes:
- auth.routes.js: 9 authentication endpoints (register, login, refresh, logout, profile, password operations, email verification)
Middleware:
- auth.middleware.js: Token authentication, email verification, account status checks
Security Features:
- bcrypt password hashing (cost 12)
- JWT access tokens (15-minute expiry)
- Refresh tokens (7-day expiry, SHA256 hashed, revocable)
- Account lockout (5 failed attempts = 15 minutes)
- Token rotation on password reset
- Email verification workflow
- Comprehensive audit logging
Scripts:
- run-migration.js: Automated database migration runner
- test-auth.js: Comprehensive test suite (10 tests)
- check-audit-log.js: Audit log verification tool
All tests passing. Production-ready implementation.
🤖 Generated with Claude Code
46 lines
1.3 KiB
JavaScript
Executable file
46 lines
1.3 KiB
JavaScript
Executable file
#!/usr/bin/env node
|
|
/**
|
|
* Database Migration Runner
|
|
*
|
|
* Usage: node scripts/run-migration.js <migration-file.sql>
|
|
* Example: node scripts/run-migration.js migrations/005_auth_system.sql
|
|
*/
|
|
|
|
import { readFileSync } from 'fs';
|
|
import { getDb } from '../config/db.js';
|
|
import { fileURLToPath } from 'url';
|
|
import { dirname, join, resolve } from 'path';
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
const migrationFile = process.argv[2];
|
|
|
|
if (!migrationFile) {
|
|
console.error('Usage: node scripts/run-migration.js <migration-file.sql>');
|
|
process.exit(1);
|
|
}
|
|
|
|
const migrationPath = resolve(join(__dirname, '..', migrationFile));
|
|
|
|
console.log(`\n📦 Running migration: ${migrationFile}\n`);
|
|
|
|
try {
|
|
const sql = readFileSync(migrationPath, 'utf-8');
|
|
const db = getDb();
|
|
|
|
// Execute entire SQL file as one block (better-sqlite3 handles multiple statements)
|
|
db.exec(sql);
|
|
|
|
console.log(`✅ Migration completed successfully!\n`);
|
|
|
|
// Show new tables
|
|
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name").all();
|
|
console.log('📊 Current database tables:');
|
|
tables.forEach(t => console.log(` - ${t.name}`));
|
|
console.log();
|
|
|
|
} catch (error) {
|
|
console.error('❌ Migration failed:', error.message);
|
|
console.error(error);
|
|
process.exit(1);
|
|
}
|