forked from adityagarwal15/JobSync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
463 lines (394 loc) Β· 14 KB
/
server.js
File metadata and controls
463 lines (394 loc) Β· 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
const express = require("express");
const nodemailer = require("nodemailer");
const path = require("path");
const cors = require("cors");
const rateLimit = require("express-rate-limit");
require("dotenv").config();
const mongoose = require("mongoose");
const flash = require("connect-flash");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const MongoStore = require("connect-mongo");
// β
node-fetch dynamic import fix for Node.js v20+
const fetch = (...args) => import("node-fetch").then(({ default: fetch }) => fetch(...args));
const Contact = require("./models/contact.js");
const Job = require("./models/job.js");
const authRouter = require("./routes/auth.routes.js");
const { optionalAuth } = require("./middleware/auth.middleware.js");
const jobFetcher = require("./services/jobFetcher.js");
const jobRouter = require("./routes/jobAPI.routes.js");
const searchRouter = require("./routes/searchAPI.routes.js");
const passport = require("passport");
require("./utils/passport.js");
const app = express();
const PORT = process.env.PORT || 10000;
app.set("trust proxy", 1); // Important for Render
// === Force HTTPS Redirect (for Render) ===
if (process.env.NODE_ENV === "production") {
app.use((req, res, next) => {
if (req.headers["x-forwarded-proto"] !== "https" && process.env.NODE_ENV === "production") {
return res.redirect("https://" + req.headers.host + req.url);
}
next();
});
}
// ========== MONGO DB SETUP ==========
async function main() {
try {
await mongoose.connect(process.env.MONGODB_URI);
console.log("β
Connected to MongoDB");
} catch (err) {
console.error("β MongoDB connection error:", err);
}
}
main();
// === CORS SETUP ===
const allowedOrigins = [
"https://jobsync-new.onrender.com",
"https://jobsyncc.netlify.app",
"http://localhost:5000",
];
// ========== MIDDLEWARE ==========
app.use(
cors({
origin: (origin, callback) => {
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error("CORS not allowed for origin: " + origin));
}
},
methods: ["GET", "POST", "PUT", "DELETE"],
credentials: true,
allowedHeaders: ["Content-Type", "Authorization", "X-CSRF-Token", "X-Requested-With"],
exposedHeaders: ["Set-Cookie"],
})
);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
// Session configuration with MongoStore and flash messages
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: process.env.MONGODB_URI,
}),
cookie: {
maxAge: 1000 * 60 * 60 * 24, // 1 day
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
},
})
);
app.use(passport.initialize());
app.use(passport.session());
// Initialize flash middleware
app.use(flash());
// Import CSRF middleware
const {
csrfProtection,
exposeCsrfToken,
csrfErrorHandler,
tokenStore,
} = require("./middleware/csrf.middleware.js");
// Apply CSRF protection selectively
const csrfMiddleware = (req, res, next) => {
console.log("π Global CSRF middleware checking:", req.path, req.method);
// Skip CSRF for safe methods (GET, HEAD, OPTIONS)
if (["GET", "HEAD", "OPTIONS"].includes(req.method)) {
console.log("π Skipping CSRF for safe method:", req.method, req.path);
return next();
}
// Skip CSRF for API routes and send-email (handled separately)
if (req.path.startsWith("/api/") || req.path === "/send-email") {
console.log("π Skipping CSRF for:", req.path);
return next();
}
console.log("π Applying CSRF protection to:", req.path);
return csrfProtection(req, res, next);
};
app.use(csrfMiddleware);
// Make flash messages available to all views
app.use((req, res, next) => {
res.locals.success = req.flash("success");
res.locals.error = req.flash("error");
res.locals.warning = req.flash("warning");
res.locals.info = req.flash("info");
next();
});
// Apply CSRF token exposure middleware
app.use(exposeCsrfToken);
// Apply CSRF error handler
app.use(csrfErrorHandler);
// === CSRF TOKEN ENDPOINTS ===
// Generate new CSRF token (for explicit requests)
app.get("/csrf-token", (req, res) => {
try {
// Generate a simple but secure CSRF token
const token =
Math.random().toString(36).substring(2) +
Date.now().toString(36) +
Math.random().toString(36).substring(2);
console.log(
"π Generating NEW CSRF token for explicit request from:",
req.get("Origin") || "Unknown origin"
);
// Store the token in our token store for validation
tokenStore.set(token, {
createdAt: Date.now(),
origin: req.get("Origin") || "Unknown",
userAgent: req.get("User-Agent"),
});
// Set the token in a cookie for the client
const cookieOptions = {
httpOnly: false, // Allow JavaScript access for testing
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
maxAge: 3600000, // 1 hour
path: "/",
};
res.cookie("_csrf", token, cookieOptions);
console.log("π NEW CSRF token generated and cookie set:", token.substring(0, 20) + "...");
console.log("π Tokens in store:", tokenStore.size);
res.json({
csrfToken: token,
message: "CSRF token generated successfully",
timestamp: new Date().toISOString(),
cookieSet: true,
});
} catch (error) {
console.error("β Error generating CSRF token:", error);
res.status(500).json({
error: "Failed to generate CSRF token",
message: error.message,
});
}
});
// Get current CSRF token (returns existing token from locals/cookie)
app.get("/csrf-token/current", (req, res) => {
try {
// Return the token that was auto-generated by the middleware
const token = res.locals.csrfToken || req.cookies._csrf;
if (token) {
console.log("π Returning current CSRF token:", token.substring(0, 20) + "...");
res.json({
csrfToken: token,
message: "Current CSRF token retrieved",
timestamp: new Date().toISOString(),
source: res.locals.csrfToken ? "middleware" : "cookie",
});
} else {
res.status(404).json({
error: "No CSRF token found",
message: "Please refresh the page to get a CSRF token",
});
}
} catch (error) {
console.error("β Error retrieving current CSRF token:", error);
res.status(500).json({
error: "Failed to retrieve CSRF token",
message: error.message,
});
}
});
// Test CSRF protection endpoint
app.post("/test-csrf", (req, res) => {
console.log("π§ͺ Test CSRF endpoint hit");
console.log("π§ͺ Request headers:", req.headers);
console.log("π§ͺ Request body:", req.body);
console.log("π§ͺ Request cookies:", req.cookies);
res.json({
success: true,
message: "CSRF protection working!",
receivedToken: req.body._csrf,
cookieToken: req.cookies._csrf,
headers: req.headers,
timestamp: new Date().toISOString(),
});
});
// Test CSRF protection WITHOUT token (should fail)
app.post("/test-csrf-no-token", (req, res) => {
console.log(
"π§ͺ Test CSRF NO TOKEN endpoint hit - this should not happen if CSRF protection works"
);
console.log("π¨ CSRF middleware should have blocked this request!");
console.log("π¨ Request path:", req.path);
console.log("π¨ Request method:", req.method);
console.log("π¨ CSRF token in body:", req.body._csrf ? "PRESENT" : "MISSING");
console.log("π¨ CSRF token in cookies:", req.cookies._csrf ? "PRESENT" : "MISSING");
console.log("π¨ CSRF token in headers:", req.headers["x-csrf-token"] ? "PRESENT" : "MISSING");
res.json({
success: false,
message: "This should not be reachable without CSRF token",
timestamp: new Date().toISOString(),
});
});
// === RATE LIMITING ===
const emailRateLimit = rateLimit({
windowMs: 15 * 60 * 1000,
max: 3,
message: {
success: false,
message: "Too many email requests from this IP. Please try again in 15 minutes.",
error: "RATE_LIMIT_EXCEEDED",
},
handler: (req, res) => {
console.log(`π¨ Email rate limit exceeded for IP: ${req.ip} at ${new Date().toISOString()}`);
res.status(429).json({
success: false,
message: "Too many email requests from this IP. Please try again in 15 minutes.",
retryAfter: Math.round(15 * 60),
});
},
});
const generalRateLimit = rateLimit({
windowMs: 60 * 1000,
max: 100,
message: {
success: false,
message: "Too many requests from this IP. Please try again later.",
error: "GENERAL_RATE_LIMIT_EXCEEDED",
},
handler: (req, res) => {
console.log(`β οΈ General rate limit exceeded for IP: ${req.ip} at ${new Date().toISOString()}`);
res.status(429).json({
success: false,
message: "Too many requests from this IP. Please try again later.",
});
},
});
app.use(generalRateLimit);
// ========== ROUTES ==========
app.get("/", optionalAuth, (req, res) => {
res.render("index.ejs");
});
app.use("/", authRouter);
app.use("/api/jobs", jobRouter);
app.use("/api/search", searchRouter);
// === PROXY EXTERNAL API TO BYPASS CORS ===
app.get("/api/totalusers", async (req, res) => {
try {
const response = await fetch("https://sc.ecombullet.com/api/dashboard/totalusers");
const data = await response.json();
res.json(data);
} catch (err) {
console.error("β External API fetch failed:", err);
res.status(500).json({ error: "Failed to fetch external data" });
}
});
// Proxy for Google Custom Search API
app.get("/api/google-search", async (req, res) => {
try {
const { q, num = 10, start = 1 } = req.query;
if (!q) {
return res.status(400).json({ error: 'Query parameter "q" is required' });
}
const apiKey = process.env.GOOGLE_CLOUD_SEARCH_API;
const engineId = process.env.GOOGLE_SEARCH_ENGINE_API;
if (!apiKey || !engineId) {
return res.status(500).json({ error: "Google API credentials not configured" });
}
const url = `https://www.googleapis.com/customsearch/v1?key=${apiKey}&cx=${engineId}&q=${encodeURIComponent(q)}&num=${num}&start=${start}`;
console.log(`Proxying Google search for: "${q}"`);
const response = await fetch(url);
if (!response.ok) {
const errorText = await response.text();
console.error(`Google API Error: ${response.status} ${response.statusText}`);
console.error(`Error Details: ${errorText}`);
return res.status(response.status).json({
error: `Google API Error: ${response.status} ${response.statusText}`,
details: errorText,
});
}
const data = await response.json();
if (data.error) {
console.error("Google API returned error:", data.error);
return res.status(400).json({ error: data.error });
}
res.json(data);
} catch (err) {
console.error("Google search proxy failed:", err);
res.status(500).json({ error: "Failed to proxy Google search request" });
}
});
// ========== EMAIL ==========
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
secure: process.env.SMTP_SECURE === "true",
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
tls: {
rejectUnauthorized: false,
},
});
// Email route with rate limiting only (CSRF excluded in middleware)
app.post("/send-email", emailRateLimit, async (req, res) => {
console.log("π© Incoming form submission:", req.body);
const { user_name, user_role, user_email, portfolio_link, message } = req.body;
if (!user_name || !user_role || !user_email || !message) {
return res.status(400).json({ success: false, message: "Please fill in all required fields." });
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(user_email)) {
return res.status(400).json({ success: false, message: "Please enter a valid email address." });
}
try {
await Contact.create({
userName: user_name,
userRole: user_role,
userEmail: user_email,
portfolioLink: portfolio_link || "undefined",
message,
});
const mailOptions = {
from: `"JobSync Contact Form" <${process.env.SMTP_SENDER}>`,
to: process.env.SMTP_SENDER,
replyTo: user_email,
subject: `New Contact Form Submission from ${user_name} - JobSync`,
html: `<p><strong>Name:</strong> ${user_name}<br>
<strong>Role:</strong> ${user_role}<br>
<strong>Email:</strong> ${user_email}<br>
<strong>Portfolio:</strong> ${portfolio_link}<br><br>
<strong>Message:</strong><br>${message.replace(/\n/g, "<br>")}</p>`,
text: `Name: ${user_name}\nRole: ${user_role}\nEmail: ${user_email}\nPortfolio: ${portfolio_link}\n\nMessage:\n${message}`,
};
const info = await transporter.sendMail(mailOptions);
console.log(`β
Email sent: ${info.messageId}`);
res.json({ success: true, message: "Email sent successfully!", messageId: info.messageId });
} catch (error) {
console.error("β Error sending email or saving to DB:", error);
res.status(500).json({ success: false, message: "Failed to send email." });
}
});
// === START SERVER ===
app.listen(PORT, async () => {
console.log(`π Server running on port ${PORT}`);
try {
const jobCount = await Job.countDocuments({ isActive: true });
const shouldRunInitialFetch = jobCount < 10;
console.log(`π Current active jobs in database: ${jobCount}`);
if (shouldRunInitialFetch) {
console.log("π Database has few jobs, running initial fetch...");
await jobFetcher.init(true);
} else {
console.log("β
Database has sufficient jobs, only scheduling cron job...");
await jobFetcher.init(false);
}
console.log("Job Fetcher Service started successfully");
} catch (error) {
console.error("Failed to start Job Fetcher Service:", error);
}
});
// 404 handler
app.use((req, res, next) => {
res.status(404).render("404");
});