Fix jwt-token

This commit is contained in:
2025-09-16 07:11:32 +02:00
parent 50f60035b5
commit fbd03aeffc
3 changed files with 31 additions and 1 deletions

View File

@@ -7,8 +7,10 @@ if (process.env.NODE_ENV !== 'test') {
// Check if models are already initialized (for tests)
if (global.__TEST_MODELS__) {
console.log(`🔧 DEBUG: Using global test models from models/index.js`);
module.exports = global.__TEST_MODELS__;
} else {
console.log(`🔧 DEBUG: Creating new models instance in models/index.js`);
// Configure database based on environment
let sequelize;
if (process.env.NODE_ENV === 'test') {

View File

@@ -207,7 +207,19 @@ async function handleDetection(req, res) {
console.log('🔍 Complete detection data:', JSON.stringify(detectionData, null, 2));
// Check if device exists and is approved
console.log(`🔍 DEBUG: Looking for device with ID: ${detectionData.device_id} (type: ${typeof detectionData.device_id})`);
console.log(`🔍 DEBUG: Device model being used:`, Device);
console.log(`🔍 DEBUG: Sequelize instance:`, Device.sequelize.constructor.name);
// Get all devices to see what's actually in the database
const allDevices = await Device.findAll();
console.log(`🔍 DEBUG: Total devices in database: ${allDevices.length}`);
allDevices.forEach(d => {
console.log(` - Device ID: ${d.id} (type: ${typeof d.id}), name: ${d.name}, approved: ${d.is_approved}, active: ${d.is_active}`);
});
let device = await Device.findOne({ where: { id: detectionData.device_id } });
console.log(`🔍 DEBUG: Device lookup result:`, device ? `Found device ${device.id}` : 'Device not found');
if (!device) {
// Device not found - reject detection and require manual registration

View File

@@ -94,6 +94,11 @@ async function setupTestEnvironment() {
// Store models globally for routes to access
global.__TEST_MODELS__ = models;
// Debug info
console.log(`🔧 DEBUG: Test sequelize instance: ${sequelize.constructor.name}`);
console.log(`🔧 DEBUG: Test database storage: ${sequelize.options.storage}`);
console.log(`🔧 DEBUG: Global models set:`, Object.keys(global.__TEST_MODELS__));
// Sync database
await sequelize.sync({ force: true });
@@ -197,14 +202,25 @@ async function createTestDevice(deviceData = {}) {
// If a specific ID is provided, use upsert to ensure it's respected
if (deviceData.id) {
console.log(`🔧 DEBUG: Creating device with specific ID: ${deviceData.id} (type: ${typeof deviceData.id})`);
console.log(`🔧 DEBUG: Device data:`, defaultDeviceData);
const [device, created] = await Device.upsert(defaultDeviceData, {
returning: true
});
console.log(`🔧 DEBUG: Device ${created ? 'created' : 'updated'}: ID=${device.id} (type: ${typeof device.id}), approved=${device.is_approved}`);
// Verify the device exists immediately after creation
const verification = await Device.findByPk(device.id);
console.log(`🔧 DEBUG: Verification lookup: ${verification ? `Found device ${verification.id}` : 'Device not found after creation!'}`);
return device;
} else {
// Auto-generate ID when none provided
defaultDeviceData.id = Math.floor(Math.random() * 1000000000);
return await Device.create(defaultDeviceData);
console.log(`🔧 DEBUG: Creating device with auto-generated ID: ${defaultDeviceData.id}`);
const device = await Device.create(defaultDeviceData);
console.log(`🔧 DEBUG: Auto-generated device created: ID=${device.id}, approved=${device.is_approved}`);
return device;
}
}