Fix jwt-token

This commit is contained in:
2025-09-20 23:11:57 +02:00
parent 4df5a628de
commit b74772f71d
3 changed files with 58 additions and 24 deletions

View File

@@ -43,6 +43,8 @@ fi
# Check if setup/migrations were successful # Check if setup/migrations were successful
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "Database initialization completed successfully" echo "Database initialization completed successfully"
# Set flag to indicate database is already initialized
export DB_INITIALIZED=true
else else
echo "Database initialization failed" echo "Database initialization failed"
exit 1 exit 1

View File

@@ -169,20 +169,24 @@ async function startServer() {
console.log('Database force synchronized.'); console.log('Database force synchronized.');
} }
// STEP 2: Run migrations after tables exist // STEP 2: Run migrations after tables exist (skip if DB_INITIALIZED is set)
try { if (!process.env.DB_INITIALIZED) {
await runMigrations(); try {
} catch (migrationError) { await runMigrations();
console.error('Migration error:', migrationError); } catch (migrationError) {
throw migrationError; // Fatal error - don't continue console.error('Migration error:', migrationError);
} throw migrationError; // Fatal error - don't continue
}
// STEP 3: Seed database with initial data // STEP 3: Seed database with initial data
try { try {
await seedDatabase(); await seedDatabase();
} catch (seedError) { } catch (seedError) {
console.error('Seeding error:', seedError); console.error('Seeding error:', seedError);
throw seedError; // Fatal error - don't continue throw seedError; // Fatal error - don't continue
}
} else {
console.log(' Database already initialized by setup script, skipping migrations and seeding');
} }
server.listen(PORT, () => { server.listen(PORT, () => {

View File

@@ -39,13 +39,32 @@ const setupDatabase = async () => {
// Create sample data // Create sample data
console.log('📊 Creating sample data...\n'); console.log('📊 Creating sample data...\n');
// Create default tenant
console.log('🏢 Creating default tenant...');
const defaultTenant = await Tenant.create({
name: 'Default Organization',
slug: 'default',
subscription_type: 'enterprise',
is_active: true,
auth_provider: 'local',
features: {
max_devices: -1,
max_users: -1,
api_rate_limit: 50000,
data_retention_days: -1,
features: ['all']
}
});
console.log(`✅ Default tenant created: ${defaultTenant.name}`);
// Create admin user // Create admin user
console.log('👤 Creating admin user...'); console.log('👤 Creating admin user...');
const adminUser = await User.create({ const adminUser = await User.create({
username: 'admin', username: 'admin',
email: 'admin@example.com', email: 'admin@example.com',
password_hash: await bcrypt.hash('admin123', 10), password_hash: await bcrypt.hash('admin123', 10),
role: 'admin' role: 'admin',
tenant_id: defaultTenant.id
}); });
console.log(`✅ Admin user created: ${adminUser.username}`); console.log(`✅ Admin user created: ${adminUser.username}`);
@@ -55,7 +74,8 @@ const setupDatabase = async () => {
username: 'operator', username: 'operator',
email: 'operator@example.com', email: 'operator@example.com',
password_hash: await bcrypt.hash('operator123', 10), password_hash: await bcrypt.hash('operator123', 10),
role: 'operator' role: 'operator',
tenant_id: defaultTenant.id
}); });
console.log(`✅ Operator user created: ${operatorUser.username}`); console.log(`✅ Operator user created: ${operatorUser.username}`);
@@ -69,7 +89,8 @@ const setupDatabase = async () => {
geo_lat: 59.3293, geo_lat: 59.3293,
geo_lon: 18.0686, geo_lon: 18.0686,
status: 'online', status: 'online',
last_seen: new Date() last_seen: new Date(),
tenant_id: defaultTenant.id
}, },
{ {
device_id: 1941875382, device_id: 1941875382,
@@ -78,7 +99,8 @@ const setupDatabase = async () => {
geo_lat: 57.7089, geo_lat: 57.7089,
geo_lon: 11.9746, geo_lon: 11.9746,
status: 'online', status: 'online',
last_seen: new Date() last_seen: new Date(),
tenant_id: defaultTenant.id
}, },
{ {
device_id: 1941875383, device_id: 1941875383,
@@ -87,7 +109,8 @@ const setupDatabase = async () => {
geo_lat: 55.6050, geo_lat: 55.6050,
geo_lon: 13.0038, geo_lon: 13.0038,
status: 'offline', status: 'offline',
last_seen: new Date(Date.now() - 2 * 60 * 60 * 1000) // 2 hours ago last_seen: new Date(Date.now() - 2 * 60 * 60 * 1000), // 2 hours ago
tenant_id: defaultTenant.id
} }
]); ]);
console.log(`✅ Created ${devices.length} sample devices`); console.log(`✅ Created ${devices.length} sample devices`);
@@ -123,7 +146,8 @@ const setupDatabase = async () => {
timestamp: new Date(), timestamp: new Date(),
threat_level: 'high', threat_level: 'high',
estimated_distance: 150, estimated_distance: 150,
requires_action: true requires_action: true,
tenant_id: defaultTenant.id
}, },
{ {
device_id: 1941875382, device_id: 1941875382,
@@ -137,7 +161,8 @@ const setupDatabase = async () => {
timestamp: new Date(Date.now() - 60 * 60 * 1000), timestamp: new Date(Date.now() - 60 * 60 * 1000),
threat_level: 'medium', threat_level: 'medium',
estimated_distance: 800, estimated_distance: 800,
requires_action: false requires_action: false,
tenant_id: defaultTenant.id
}, },
{ {
device_id: 1941875381, device_id: 1941875381,
@@ -151,7 +176,8 @@ const setupDatabase = async () => {
timestamp: new Date(Date.now() - 2 * 60 * 60 * 1000), timestamp: new Date(Date.now() - 2 * 60 * 60 * 1000),
threat_level: 'low', threat_level: 'low',
estimated_distance: 2500, estimated_distance: 2500,
requires_action: false requires_action: false,
tenant_id: defaultTenant.id
} }
]); ]);
console.log(`✅ Created ${detections.length} sample drone detections`); console.log(`✅ Created ${detections.length} sample drone detections`);
@@ -177,7 +203,8 @@ const setupDatabase = async () => {
channels: ['sms', 'email'] channels: ['sms', 'email']
}, },
cooldown_minutes: 2, cooldown_minutes: 2,
is_active: true is_active: true,
tenant_id: defaultTenant.id
}, },
{ {
user_id: operatorUser.id, user_id: operatorUser.id,
@@ -196,7 +223,8 @@ const setupDatabase = async () => {
channels: ['sms'] channels: ['sms']
}, },
cooldown_minutes: 10, cooldown_minutes: 10,
is_active: true is_active: true,
tenant_id: defaultTenant.id
}, },
{ {
user_id: adminUser.id, user_id: adminUser.id,