From 91cfe9dd503b6f7d6557e4831a96d2fe22f7d370 Mon Sep 17 00:00:00 2001 From: Chandini Date: Fri, 19 Sep 2025 11:07:52 +0530 Subject: [PATCH] backend changes --- config/urls.js | 9 +- docker-compose.yml | 5 +- services/api-gateway/.env .prod | 7 +- services/user-auth/src/routes/auth.js | 104 +++++++++++++++++- .../user-auth/src/services/authService.js | 41 ++++--- services/web-dashboard/src/services/api.js | 6 +- 6 files changed, 132 insertions(+), 40 deletions(-) diff --git a/config/urls.js b/config/urls.js index 522ef9f..7e1afd9 100644 --- a/config/urls.js +++ b/config/urls.js @@ -6,13 +6,14 @@ // ======================================== // LIVE PRODUCTION URLS (Currently Active) // ======================================== -const FRONTEND_URL = 'http://192.168.1.31:3001'; +const FRONTEND_URL = 'https://dashboard.codenuk.com'; const BACKEND_URL = 'https://backend.codenuk.com'; - // ======================================== -// const FRONTEND_URL = 'http://localhost:3001'; -// const BACKEND_URL = 'http://localhost:8000'; +// LOCAL DEVELOPMENT URLS +// ======================================== +// const FRONTEND_URL = 'http://192.168.1.16:3001'; +// const BACKEND_URL = 'http://192.168.1.16:8000'; // ======================================== // CORS CONFIGURATION (Auto-generated) diff --git a/docker-compose.yml b/docker-compose.yml index abdac7f..c02b4d4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -233,8 +233,7 @@ services: - NODE_ENV=development - PORT=8000 - HOST=0.0.0.0 - - FRONTEND_URL=http://192.168.1.31:3001 # Allow all URLs - - CORS_ORIGINS=* # Allow all URLs + - CORS_ORIGINS=https://dashboard.codenuk.com - CORS_METHODS=GET,POST,PUT,DELETE,PATCH,OPTIONS # Add this line - CORS_CREDENTIALS=true # Add this line # Database connections @@ -507,7 +506,6 @@ services: - JWT_ACCESS_EXPIRY=24h - JWT_ADMIN_ACCESS_EXPIRY=7d - JWT_REFRESH_EXPIRY=7d - - FRONTEND_URL=http://192.168.1.31:3001 # Email Configuration - SMTP_HOST=smtp.gmail.com - SMTP_PORT=587 @@ -613,7 +611,6 @@ services: environment: - PORT=8012 - HOST=0.0.0.0 - - FRONTEND_URL=http://192.168.1.31:3001 - POSTGRES_HOST=postgres - POSTGRES_PORT=5432 - POSTGRES_DB=dev_pipeline diff --git a/services/api-gateway/.env .prod b/services/api-gateway/.env .prod index 5a6f40e..7e542a5 100644 --- a/services/api-gateway/.env .prod +++ b/services/api-gateway/.env .prod @@ -28,9 +28,10 @@ RABBITMQ_USER=pipeline_admin RABBITMQ_PASSWORD=secure_rabbitmq_password # CORS -FRONTEND_URL=http://192.168.1.31:3001 +FRONTEND_URL=http://192.168.1.16:3001 # CORS Configuration -CORS_ORIGIN=http://192.168.1.31:3001 -CORS_METHODS=GET,POST,PUT,DELETE,PATCH,OPTIONS +CORS_ORIGIN=http://192.168.1.16:3001 +CORS_METHODS=GET,POST,PUT,DELETE,PATCH,OPT +IONS CORS_CREDENTIALS=true \ No newline at end of file diff --git a/services/user-auth/src/routes/auth.js b/services/user-auth/src/routes/auth.js index 0e763c7..9700af6 100644 --- a/services/user-auth/src/routes/auth.js +++ b/services/user-auth/src/routes/auth.js @@ -63,25 +63,121 @@ router.post('/register', registerRateLimit, validateRegistration, async (req, re router.get('/verify-email', async (req, res) => { try { const { token } = req.query; + + if (!token) { + // Use centralized config instead of environment variables + let frontendUrl; + try { + const urls = require('../../../../config/urls'); + frontendUrl = urls.FRONTEND_URL || 'http://192.168.1.16:3001'; + } catch (err) { + frontendUrl = 'http://192.168.1.16:3001'; + } + const redirectUrl = `${frontendUrl}/signin?error=${encodeURIComponent('Verification token is required')}`; + if (req.query.format === 'json') { + return res.status(400).json({ success: false, message: 'Verification token is required', redirect: redirectUrl }); + } + return res.redirect(302, redirectUrl); + } + await authService.verifyEmailToken(token); - const frontendUrl = process.env.FRONTEND_URL || 'http://192.168.1.31:3001'; + // Use centralized config instead of environment variables + let frontendUrl; + try { + const urls = require('../../../../config/urls'); + frontendUrl = urls.FRONTEND_URL || 'http://192.168.1.16:3001'; + } catch (err) { + frontendUrl = 'http://192.168.1.16:3001'; + } const redirectUrl = `${frontendUrl}/signin?verified=true`; + + console.log(`✅ Email verification successful, redirecting to: ${redirectUrl}`); + // Prefer redirect by default; only return JSON if explicitly requested if (req.query.format === 'json') { - return res.json({ success: true, message: 'Email verified successfully', redirect: redirectUrl }); + return res.json({ + success: true, + message: 'Email verified successfully', + redirect: redirectUrl + }); } return res.redirect(302, redirectUrl); } catch (error) { - const frontendUrl = process.env.FRONTEND_URL || 'http://192.168.1.31:3001'; + // Use centralized config instead of environment variables + let frontendUrl; + try { + const urls = require('../../../../config/urls'); + frontendUrl = urls.FRONTEND_URL || 'http://192.168.1.16:3001'; + } catch (err) { + frontendUrl = 'http://192.168.1.16:3001'; + } const redirectUrl = `${frontendUrl}/signin?error=${encodeURIComponent(error.message)}`; + + console.error(`❌ Email verification failed: ${error.message}, redirecting to: ${redirectUrl}`); + if (req.query.format === 'json') { - return res.status(400).json({ success: false, message: error.message, redirect: redirectUrl }); + return res.status(400).json({ + success: false, + message: error.message, + redirect: redirectUrl + }); } return res.redirect(302, redirectUrl); } }); +// POST /api/auth/resend-verification - Resend verification email +router.post('/resend-verification', async (req, res) => { + try { + const { email } = req.body; + + if (!email) { + return res.status(400).json({ + success: false, + error: 'Email is required', + message: 'Please provide an email address' + }); + } + + // Find user by email + const user = await User.findByEmail(email); + if (!user) { + return res.status(404).json({ + success: false, + error: 'User not found', + message: 'No account found with this email address' + }); + } + + // Check if already verified + if (user.email_verified) { + return res.status(400).json({ + success: false, + error: 'Already verified', + message: 'This email address is already verified' + }); + } + + // Send verification email + await authService.sendVerificationEmail(user); + + console.log(`📧 Verification email resent to: ${email}`); + + res.json({ + success: true, + message: 'Verification email sent successfully. Please check your inbox.' + }); + } catch (error) { + console.error('❌ Resend verification failed:', error.message); + res.status(500).json({ + success: false, + error: 'Failed to resend verification', + message: error.message + }); + } +}); + // POST /api/auth/login - User login router.post('/login', loginRateLimit , validateLogin, async (req, res) => { try { diff --git a/services/user-auth/src/services/authService.js b/services/user-auth/src/services/authService.js index 1ce8572..1415271 100644 --- a/services/user-auth/src/services/authService.js +++ b/services/user-auth/src/services/authService.js @@ -148,32 +148,29 @@ class AuthService { async sendVerificationEmail(user) { const token = await this.createEmailVerificationToken(user.id); - // Resolve verification URL. Prefer environment variable (works in Docker). If not present, - // fall back to the repository-level config/urls.js when available (development). + // Use centralized URL configuration - no environment variables needed let verifyUrl; - const frontendUrlFromEnv = process.env.FRONTEND_URL; - if (frontendUrlFromEnv) { - const FRONTEND_URL = frontendUrlFromEnv.replace(/\/$/, ''); - verifyUrl = `${FRONTEND_URL}/verify-email?token=${encodeURIComponent(token)}`; - } else { - try { - // Attempt to load repo-level config (works when running locally from repo root) - // This is guarded so it won't crash inside Docker if the relative path isn't valid. - // eslint-disable-next-line global-require - const urls = require('../../../../config/urls'); - if (urls && typeof urls.getVerificationUrl === 'function') { - verifyUrl = urls.getVerificationUrl(token); - } else if (urls && urls.FRONTEND_URL) { - const FRONTEND_URL = urls.FRONTEND_URL.replace(/\/$/, ''); - verifyUrl = `${FRONTEND_URL}/verify-email?token=${encodeURIComponent(token)}`; - } - } catch (err) { - // As a last resort, build a relative backend-hosted verification endpoint - const backendHost = process.env.BACKEND_URL || `http://localhost:${process.env.PORT || 8011}`; - verifyUrl = `${backendHost.replace(/\/$/, '')}/api/auth/verify-email?token=${encodeURIComponent(token)}`; + try { + // Load centralized config from repository root + // eslint-disable-next-line global-require + const urls = require('../../../../config/urls'); + if (urls && typeof urls.getVerificationUrl === 'function') { + verifyUrl = urls.getVerificationUrl(token); + } else if (urls && urls.FRONTEND_URL) { + const FRONTEND_URL = urls.FRONTEND_URL.replace(/\/$/, ''); + verifyUrl = `${FRONTEND_URL}/verify-email?token=${encodeURIComponent(token)}`; + } else { + // Hardcoded fallback - no environment variables + verifyUrl = `http://192.168.1.16:3001/verify-email?token=${encodeURIComponent(token)}`; } + } catch (err) { + // Hardcoded fallback - no environment variables + verifyUrl = `http://192.168.1.16:3001/verify-email?token=${encodeURIComponent(token)}`; } + console.log(`📧 Generated verification URL: ${verifyUrl}`); + console.log(`📧 Using centralized URL config`); + const today = new Date(); const dateString = today.toLocaleDateString('en-US'); diff --git a/services/web-dashboard/src/services/api.js b/services/web-dashboard/src/services/api.js index b407b59..7dd0d18 100644 --- a/services/web-dashboard/src/services/api.js +++ b/services/web-dashboard/src/services/api.js @@ -241,7 +241,7 @@ import axios from 'axios'; // Configure API client for requirement processor const apiClient = axios.create({ - baseURL: 'http://localhost:8001', // Direct to requirement processor + baseURL: 'https://backend.codenuk.com/api/requirements', // Via API gateway timeout: 30000, headers: { 'Content-Type': 'application/json', @@ -250,7 +250,7 @@ const apiClient = axios.create({ // Configure API client for Template-Manager service const templateApiClient = axios.create({ - baseURL: 'http://localhost:8009', // Direct to template-manager + baseURL: 'https://backend.codenuk.com/api/templates', // Via API gateway timeout: 30000, headers: { 'Content-Type': 'application/json', @@ -259,7 +259,7 @@ const templateApiClient = axios.create({ // Configure API client for User-Auth service const authApiClient = axios.create({ - baseURL: 'http://localhost:8011', // Direct to user-auth + baseURL: 'https://backend.codenuk.com/api/auth', // Via API gateway timeout: 30000, headers: { 'Content-Type': 'application/json',