Fix jwt-token
This commit is contained in:
@@ -11,6 +11,7 @@ require('dotenv').config();
|
||||
const { sequelize } = require('./models');
|
||||
const routes = require('./routes');
|
||||
const { initializeSocketHandlers } = require('./services/socketService');
|
||||
const AlertService = require('./services/alertService');
|
||||
const errorHandler = require('./middleware/errorHandler');
|
||||
|
||||
const app = express();
|
||||
@@ -81,8 +82,24 @@ async function startServer() {
|
||||
}
|
||||
|
||||
server.listen(PORT, () => {
|
||||
console.log(`Server running on port ${PORT}`);
|
||||
console.log(`Environment: ${process.env.NODE_ENV}`);
|
||||
console.log('\n🚀 Drone Detection System Started Successfully!');
|
||||
console.log('================================================');
|
||||
console.log(`📊 Environment: ${process.env.NODE_ENV || 'development'}`);
|
||||
console.log(`🌐 Server Port: ${PORT}`);
|
||||
console.log(`💾 Database: ${process.env.DB_HOST}:${process.env.DB_PORT}`);
|
||||
console.log(`🔴 Redis: ${process.env.REDIS_HOST || 'localhost'}:${process.env.REDIS_PORT || 6379}`);
|
||||
|
||||
// Initialize AlertService to check SMS status
|
||||
const alertService = new AlertService();
|
||||
if (alertService.twilioEnabled) {
|
||||
console.log('📱 SMS Alerts: ✅ Enabled');
|
||||
} else {
|
||||
console.log('📱 SMS Alerts: ⚠️ Disabled (no Twilio credentials)');
|
||||
}
|
||||
|
||||
console.log(`📊 Health check: http://localhost:${PORT}/health`);
|
||||
console.log(`🌐 API endpoint: http://localhost:${PORT}/api`);
|
||||
console.log('================================================\n');
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Unable to start server:', error);
|
||||
|
||||
@@ -5,6 +5,8 @@ const { Op } = require('sequelize');
|
||||
class AlertService {
|
||||
constructor() {
|
||||
this.twilioClient = null;
|
||||
this.twilioPhone = null;
|
||||
this.twilioEnabled = false;
|
||||
this.initializeTwilio();
|
||||
}
|
||||
|
||||
@@ -89,13 +91,40 @@ class AlertService {
|
||||
}
|
||||
|
||||
initializeTwilio() {
|
||||
if (process.env.TWILIO_ACCOUNT_SID && process.env.TWILIO_AUTH_TOKEN) {
|
||||
this.twilioClient = twilio(
|
||||
process.env.TWILIO_ACCOUNT_SID,
|
||||
process.env.TWILIO_AUTH_TOKEN
|
||||
);
|
||||
} else {
|
||||
console.warn('Twilio credentials not configured. SMS alerts will be disabled.');
|
||||
// Check if Twilio credentials are provided
|
||||
const accountSid = process.env.TWILIO_ACCOUNT_SID;
|
||||
const authToken = process.env.TWILIO_AUTH_TOKEN;
|
||||
const phoneNumber = process.env.TWILIO_PHONE_NUMBER;
|
||||
|
||||
// If any Twilio credential is missing, disable SMS functionality
|
||||
if (!accountSid || !authToken || !phoneNumber ||
|
||||
accountSid.trim() === '' || authToken.trim() === '' || phoneNumber.trim() === '') {
|
||||
console.log('📱 Twilio credentials not configured - SMS alerts disabled');
|
||||
console.log('ℹ️ To enable SMS alerts, set TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, and TWILIO_PHONE_NUMBER');
|
||||
this.twilioEnabled = false;
|
||||
this.twilioClient = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate Twilio Account SID format
|
||||
if (!accountSid.startsWith('AC')) {
|
||||
console.log('⚠️ Invalid Twilio Account SID format - SMS alerts disabled');
|
||||
console.log('ℹ️ Account SID must start with "AC"');
|
||||
this.twilioEnabled = false;
|
||||
this.twilioClient = null;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this.twilioClient = twilio(accountSid, authToken);
|
||||
this.twilioPhone = phoneNumber;
|
||||
this.twilioEnabled = true;
|
||||
console.log('📱 Twilio SMS service initialized successfully');
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to initialize Twilio:', error.message);
|
||||
console.log('📱 SMS alerts disabled due to Twilio initialization error');
|
||||
this.twilioEnabled = false;
|
||||
this.twilioClient = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -343,27 +372,63 @@ class AlertService {
|
||||
}
|
||||
|
||||
async sendSMSAlert(phoneNumber, message, rule, detection) {
|
||||
if (!this.twilioClient) {
|
||||
throw new Error('Twilio not configured');
|
||||
// Check if Twilio is enabled
|
||||
if (!this.twilioEnabled || !this.twilioClient) {
|
||||
console.log('📱 SMS alert skipped - Twilio not configured');
|
||||
console.log(`📱 Would have sent to ${phoneNumber}: ${message}`);
|
||||
|
||||
return await AlertLog.create({
|
||||
alert_rule_id: rule.id,
|
||||
detection_id: detection.id,
|
||||
alert_type: 'sms',
|
||||
recipient: phoneNumber,
|
||||
message: message,
|
||||
status: 'failed',
|
||||
sent_at: new Date(),
|
||||
external_id: null,
|
||||
priority: rule.priority,
|
||||
error_message: 'SMS service not configured'
|
||||
});
|
||||
}
|
||||
|
||||
const twilioMessage = await this.twilioClient.messages.create({
|
||||
body: message,
|
||||
from: process.env.TWILIO_PHONE_NUMBER,
|
||||
to: phoneNumber
|
||||
});
|
||||
try {
|
||||
console.log(`📱 Sending SMS alert to ${phoneNumber}`);
|
||||
|
||||
const twilioMessage = await this.twilioClient.messages.create({
|
||||
body: message,
|
||||
from: this.twilioPhone,
|
||||
to: phoneNumber
|
||||
});
|
||||
|
||||
return await AlertLog.create({
|
||||
alert_rule_id: rule.id,
|
||||
detection_id: detection.id,
|
||||
alert_type: 'sms',
|
||||
recipient: phoneNumber,
|
||||
message: message,
|
||||
status: 'sent',
|
||||
sent_at: new Date(),
|
||||
external_id: twilioMessage.sid,
|
||||
priority: rule.priority
|
||||
});
|
||||
console.log(`✅ SMS sent successfully: ${twilioMessage.sid}`);
|
||||
|
||||
return await AlertLog.create({
|
||||
alert_rule_id: rule.id,
|
||||
detection_id: detection.id,
|
||||
alert_type: 'sms',
|
||||
recipient: phoneNumber,
|
||||
message: message,
|
||||
status: 'sent',
|
||||
sent_at: new Date(),
|
||||
external_id: twilioMessage.sid,
|
||||
priority: rule.priority
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to send SMS:', error.message);
|
||||
|
||||
return await AlertLog.create({
|
||||
alert_rule_id: rule.id,
|
||||
detection_id: detection.id,
|
||||
alert_type: 'sms',
|
||||
recipient: phoneNumber,
|
||||
message: message,
|
||||
status: 'failed',
|
||||
sent_at: new Date(),
|
||||
external_id: null,
|
||||
priority: rule.priority,
|
||||
error_message: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async sendEmailAlert(email, message, rule, detection) {
|
||||
|
||||
Reference in New Issue
Block a user