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
if [ $? -eq 0 ]; then
echo "Database initialization completed successfully"
# Set flag to indicate database is already initialized
export DB_INITIALIZED=true
else
echo "Database initialization failed"
exit 1

View File

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

View File

@@ -39,13 +39,32 @@ const setupDatabase = async () => {
// Create sample data
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
console.log('👤 Creating admin user...');
const adminUser = await User.create({
username: 'admin',
email: 'admin@example.com',
password_hash: await bcrypt.hash('admin123', 10),
role: 'admin'
role: 'admin',
tenant_id: defaultTenant.id
});
console.log(`✅ Admin user created: ${adminUser.username}`);
@@ -55,7 +74,8 @@ const setupDatabase = async () => {
username: 'operator',
email: 'operator@example.com',
password_hash: await bcrypt.hash('operator123', 10),
role: 'operator'
role: 'operator',
tenant_id: defaultTenant.id
});
console.log(`✅ Operator user created: ${operatorUser.username}`);
@@ -69,7 +89,8 @@ const setupDatabase = async () => {
geo_lat: 59.3293,
geo_lon: 18.0686,
status: 'online',
last_seen: new Date()
last_seen: new Date(),
tenant_id: defaultTenant.id
},
{
device_id: 1941875382,
@@ -78,7 +99,8 @@ const setupDatabase = async () => {
geo_lat: 57.7089,
geo_lon: 11.9746,
status: 'online',
last_seen: new Date()
last_seen: new Date(),
tenant_id: defaultTenant.id
},
{
device_id: 1941875383,
@@ -87,7 +109,8 @@ const setupDatabase = async () => {
geo_lat: 55.6050,
geo_lon: 13.0038,
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`);
@@ -123,7 +146,8 @@ const setupDatabase = async () => {
timestamp: new Date(),
threat_level: 'high',
estimated_distance: 150,
requires_action: true
requires_action: true,
tenant_id: defaultTenant.id
},
{
device_id: 1941875382,
@@ -137,7 +161,8 @@ const setupDatabase = async () => {
timestamp: new Date(Date.now() - 60 * 60 * 1000),
threat_level: 'medium',
estimated_distance: 800,
requires_action: false
requires_action: false,
tenant_id: defaultTenant.id
},
{
device_id: 1941875381,
@@ -151,7 +176,8 @@ const setupDatabase = async () => {
timestamp: new Date(Date.now() - 2 * 60 * 60 * 1000),
threat_level: 'low',
estimated_distance: 2500,
requires_action: false
requires_action: false,
tenant_id: defaultTenant.id
}
]);
console.log(`✅ Created ${detections.length} sample drone detections`);
@@ -177,7 +203,8 @@ const setupDatabase = async () => {
channels: ['sms', 'email']
},
cooldown_minutes: 2,
is_active: true
is_active: true,
tenant_id: defaultTenant.id
},
{
user_id: operatorUser.id,
@@ -196,7 +223,8 @@ const setupDatabase = async () => {
channels: ['sms']
},
cooldown_minutes: 10,
is_active: true
is_active: true,
tenant_id: defaultTenant.id
},
{
user_id: adminUser.id,