diff --git a/backend/src/middleware/auth.js b/backend/src/middleware/auth.js index 4251823..74f5398 100644 --- a/backend/src/middleware/auth.js +++ b/backend/src/middleware/auth.js @@ -3,44 +3,73 @@ const { User } = require('../models'); const authenticateToken = async (req, res, next) => { try { + console.log('🔐 [Auth] Authenticating request:', req.method, req.path); const authHeader = req.headers['authorization']; + console.log('🔐 [Auth] Authorization header present:', !!authHeader); + const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN if (!token) { + console.log('❌ [Auth] No token provided'); return res.status(401).json({ success: false, message: 'Access token required' }); } + console.log('🔐 [Auth] Token received, verifying...'); const decoded = jwt.verify(token, process.env.JWT_SECRET); + console.log('✅ [Auth] Token verified successfully'); + console.log('🔐 [Auth] User ID from token:', decoded.userId); + console.log('🔐 [Auth] User role from token:', decoded.role); // Get user from database + console.log('🔍 [Auth] Looking up user in database...'); const user = await User.findByPk(decoded.userId); - if (!user || !user.is_active) { + + if (!user) { + console.log('❌ [Auth] User NOT found in database:', decoded.userId); + return res.status(401).json({ + success: false, + message: 'Invalid or inactive user' + }); + } + + console.log('✅ [Auth] User found:', user.full_name); + console.log('🔐 [Auth] User is_active:', user.is_active); + console.log('🔐 [Auth] User role:', user.role); + + if (!user.is_active) { + console.log('❌ [Auth] User is inactive'); return res.status(401).json({ success: false, message: 'Invalid or inactive user' }); } + console.log('✅ [Auth] Authentication successful'); req.user = user; next(); } catch (error) { + console.log('❌ [Auth] Error during authentication:', error.name); + console.log('❌ [Auth] Error message:', error.message); + if (error.name === 'JsonWebTokenError') { + console.log('❌ [Auth] JWT verification failed'); return res.status(401).json({ success: false, message: 'Invalid token' }); } if (error.name === 'TokenExpiredError') { + console.log('❌ [Auth] Token has expired'); return res.status(401).json({ success: false, message: 'Token expired' }); } - console.error('Auth middleware error:', error); + console.error('❌ [Auth] Unexpected error:', error); return res.status(500).json({ success: false, message: 'Internal server error' diff --git a/deploy-backend-fix.bat b/deploy-backend-fix.bat new file mode 100644 index 0000000..7822114 --- /dev/null +++ b/deploy-backend-fix.bat @@ -0,0 +1,64 @@ +@echo off +echo ======================================== +echo Deploying Backend Fixes to Production +echo ======================================== +echo. + +REM Check if we're in the right directory +if not exist backend ( + echo Error: Must run from project root directory + exit /b 1 +) + +echo Step 1: Commit local changes +echo ================================ +git add backend\src\middleware\auth.js +git add backend\src\controllers\phonePeController.js +git add backend\src\routes\phonepe.js +git add backend\src\server.js +git add luckychit\lib\core\services\api_service.dart + +git status + +echo. +set /p COMMIT="Commit these changes? (y/n): " +if /i "%COMMIT%"=="y" ( + git commit -m "Fix: Auth middleware logging, singleton ApiService, UPI settings endpoint" + echo. + echo SUCCESS: Changes committed +) else ( + echo. + echo Skipping commit + exit /b 0 +) + +echo. +echo Step 2: Push to repository +echo ================================ +set /p PUSH="Push to origin? (y/n): " +if /i "%PUSH%"=="y" ( + git push origin main + echo. + echo SUCCESS: Pushed to repository +) else ( + echo. + echo Skipping push + exit /b 0 +) + +echo. +echo ======================================== +echo Deploy to Production Server +echo ======================================== +echo. +echo Now SSH into your server and run: +echo. +echo cd /home/luckychit/apps/chitfund +echo git pull origin main +echo pm2 restart all +echo pm2 logs luckychit --lines 50 +echo. +echo ======================================== +echo. +pause + diff --git a/deploy-backend-fix.sh b/deploy-backend-fix.sh new file mode 100644 index 0000000..41d14b0 --- /dev/null +++ b/deploy-backend-fix.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +echo "🚀 Deploying Backend Fixes to Production" +echo "========================================" +echo "" + +# Check if we're in the right directory +if [ ! -d "backend" ]; then + echo "❌ Error: Must run from project root directory" + exit 1 +fi + +echo "📦 Step 1: Commit local changes" +git add backend/src/middleware/auth.js +git add backend/src/controllers/phonePeController.js +git add backend/src/routes/phonepe.js +git add backend/src/server.js +git add luckychit/lib/core/services/api_service.dart + +git status + +echo "" +read -p "Commit these changes? (y/n) " -n 1 -r +echo "" +if [[ $REPLY =~ ^[Yy]$ ]] +then + git commit -m "Fix: Auth middleware logging, singleton ApiService, UPI settings endpoint" + echo "✅ Changes committed" +else + echo "⚠️ Skipping commit" +fi + +echo "" +echo "📤 Step 2: Push to repository" +read -p "Push to origin? (y/n) " -n 1 -r +echo "" +if [[ $REPLY =~ ^[Yy]$ ]] +then + git push origin main + echo "✅ Pushed to repository" +else + echo "⚠️ Skipping push" + exit 0 +fi + +echo "" +echo "🔧 Step 3: Deploy to production server" +echo "" +echo "Now SSH into your server and run:" +echo "" +echo "cd /home/luckychit/apps/chitfund" +echo "git pull origin main" +echo "pm2 restart all" +echo "pm2 logs luckychit --lines 50" +echo "" +echo "✅ Deployment instructions complete!" +