backend changes

This commit is contained in:
Chandini 2025-09-19 11:07:52 +05:30
parent a9964e906d
commit 91cfe9dd50
6 changed files with 132 additions and 40 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -148,17 +148,10 @@ 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.
// Load centralized config from repository root
// eslint-disable-next-line global-require
const urls = require('../../../../config/urls');
if (urls && typeof urls.getVerificationUrl === 'function') {
@ -166,14 +159,18 @@ class AuthService {
} 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) {
// 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)}`;
}
// 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');

View File

@ -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',