-
Notifications
You must be signed in to change notification settings - Fork 401
Fix: Corrected routing for 'Login as Vigy' button (Issue #2638) #2658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,29 +9,80 @@ const authRoutes = require("./routes/authRoutes"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const userRoutes = require("./routes/userRoutes"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const passwordResetRoutes = require("./routes/passwordResetRoutes"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const adminRegistrationRoutes = require("./routes/adminRegistrationRoutes"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const vigyRoutes = require("./routes/vigyRoutes"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const subscribeRoute = require("./routes/subscribe"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const app = express(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Middleware | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use(cors()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Middleware setup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use(express.json()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use(cors({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
origin: process.env.FRONTEND_URL || "http://localhost:3000", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
credentials: true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
})); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
session({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
secret: "Our little secret.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
secret: process.env.SESSION_SECRET || "Our little secret.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
resave: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
23
to
26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Require SESSION_SECRET in production. Avoid weak default secrets in prod. app.use(
session({
- secret: process.env.SESSION_SECRET || "Our little secret.",
+ secret: (() => {
+ if (process.env.NODE_ENV === "production" && !process.env.SESSION_SECRET) {
+ throw new Error("SESSION_SECRET must be set in production");
+ }
+ return process.env.SESSION_SECRET || "Our little secret.";
+ })(), 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
saveUninitialized: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cookie: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
secure: process.env.NODE_ENV === 'production', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
httpOnly: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
maxAge: 24 * 60 * 60 * 1000 // 24 hours | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use(passport.initialize()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use(passport.session()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Routes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Root route for testing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.get("/", (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: "VigyBag Backend Server is Running! 🚀", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
status: "success", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
timestamp: new Date().toISOString(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
endpoints: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vigy: "/api/vigy", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
auth: "/auth", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
admin: "/api/v1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Route setup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use("/api/subscribe", subscribeRoute); // POST /api/subscribe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use("/auth", authRoutes); // /auth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use("/api", routes); // general /api routes (e.g. products) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use("/api", passwordResetRoutes); // /api/password-reset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use("/vpi", userRoutes); // /vpi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use("/api/v1", adminRegistrationRoutes); // /api/v1/admin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use("/auth", authRoutes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use("/api", routes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use("/api", passwordResetRoutes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use("/vpi", userRoutes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use("/api/v1", adminRegistrationRoutes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.use("/api/vigy", vigyRoutes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+59
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainQuick sanity check for the PR goal (frontend route). Ensure the “Login as Vigy” link points to the intended path (e.g., /vigy-login) and no stale /vigy-form links remain. 🏁 Script executed: #!/bin/bash
# Search frontend for Vigy login routes/links
rg -n -C2 -S --glob '!**/node_modules/**' --type-add 'jsx:*.{js,jsx,ts,tsx}' --type jsx \
-e 'vigy[-_/ ]login' -e 'vigy-login' -e '/vigy-login' -e '/vigy-form' -e 'Login as Vigy' Length of output: 3362 Login link correct; remove stale “/vigy-form” references
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Test route for Vigy API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.get("/api/vigy/test", (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: "Vigy API is working! ✅", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
status: "success", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
endpoint: "/api/vigy/test" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Test route for Google OAuth configuration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app.get("/auth/google/test", (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const hasClientId = !!process.env.GOOGLE_CLIENT_ID; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const hasClientSecret = !!process.env.GOOGLE_CLIENT_SECRET; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const callbackUrl = process.env.GOOGLE_CALLBACK_URL || "http://localhost:5000/auth/google/callback"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: "Google OAuth Configuration Test", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
status: hasClientId && hasClientSecret ? "configured" : "missing_credentials", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
configuration: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
hasClientId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
hasClientSecret, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
callbackUrl, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
frontendUrl: process.env.FRONTEND_URL || "http://localhost:3000" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+71
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Info disclosure risk: Google OAuth config test should not be public in prod. Leaking presence/URLs of OAuth config aids reconnaissance. -app.get("/auth/google/test", (req, res) => {
+if (process.env.NODE_ENV !== "production") app.get("/auth/google/test", (req, res) => {
const hasClientId = !!process.env.GOOGLE_CLIENT_ID;
const hasClientSecret = !!process.env.GOOGLE_CLIENT_SECRET;
const callbackUrl = process.env.GOOGLE_CALLBACK_URL || "http://localhost:5000/auth/google/callback";
res.json({
message: "Google OAuth Configuration Test",
status: hasClientId && hasClientSecret ? "configured" : "missing_credentials",
configuration: {
hasClientId,
hasClientSecret,
callbackUrl,
frontendUrl: process.env.FRONTEND_URL || "http://localhost:3000"
}
});
-});
+}); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
module.exports = app; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
require('dotenv').config(); | ||
|
||
const config = { | ||
port: process.env.PORT || 3000, | ||
port: process.env.PORT || 5000, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainPort default changed to 5000 — verify server wiring and callbacks. Ensure the HTTP server actually uses this config value and update any hardcoded localhost ports (OAuth callbacks, CORS, frontend API base URLs). 🏁 Script executed: #!/bin/bash
# Is config.port used by the server?
rg -nP --glob '!**/node_modules/**' '\b(config\.port|process\.env\.PORT)\b' backend -C2
# Find hardcoded localhost ports (3000/5000) across repo
rg -nP --glob '!**/node_modules/**' 'localhost:(3000|5000)' -C2
# Common Google OAuth callback strings to verify
rg -nP --glob '!**/node_modules/**' 'google.*callback|/auth/google/callback' backend -C3 Length of output: 5660 Unify port defaults—remove hardcoded localhost URLs • backend/routes/ContactRoute.js (line 7): import 🤖 Prompt for AI Agents
|
||
mongoURI: process.env.MONGO_URI || 'mongodb://localhost:27017/myapp', | ||
jwtSecret: process.env.JWT_SECRET || 'mysecretkey', | ||
sessionSecret: process.env.SESSION_SECRET || 'defaultsessionsecret', | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,7 +9,8 @@ const connectDB = async () => { | |||||||||||||||||||||||||
console.log("Connected to MongoDB"); | ||||||||||||||||||||||||||
} catch (err) { | ||||||||||||||||||||||||||
console.error("Error connecting to MongoDB:", err.message); | ||||||||||||||||||||||||||
process.exit(1); | ||||||||||||||||||||||||||
console.log("⚠️ MongoDB not available. Using in-memory fallback for development."); | ||||||||||||||||||||||||||
// Don't exit for development - let the app continue | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
Comment on lines
+12
to
14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainConfirm an actual in-memory fallback exists and is wired. This file only logs; ensure routes use a real store in dev. 🏁 Script executed: #!/bin/bash
# Look for any in-memory store used by Vigy routes
rg -nP "(in-?memory|memory store|fallback).*vigy" backend -C3
rg -nP "router\\.|app\\.use\\(.*/api/vigy" -C2 backend Length of output: 4643 Implement actual in-memory MongoDB fallback in development (backend/config/db.js:12–14) 🤖 Prompt for AI Agents
🛠️ Refactor suggestion Don't silently continue on DB failure in non-dev. Gate by NODE_ENV. Exiting in prod prevents partial-start bugs. } catch (err) {
console.error("Error connecting to MongoDB:", err.message);
- console.log("⚠️ MongoDB not available. Using in-memory fallback for development.");
- // Don't exit for development - let the app continue
+ if (process.env.NODE_ENV !== "development" && process.env.NODE_ENV !== "test") {
+ console.error("MongoDB unavailable. Exiting (non-dev).");
+ process.exit(1);
+ }
+ console.warn("⚠️ MongoDB not available. Using in-memory fallback for development.");
+ // Keep the app running only in dev/test
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,227 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const Vigy = require('../models/Vigy'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const jwt = require('jsonwebtoken'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Generate JWT Token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const generateToken = (vigyId) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return jwt.sign({ vigyId }, process.env.JWT_SECRET || 'mysecretkey', { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expiresIn: '7d' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+5
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eliminate insecure JWT secret fallback Hard-coding a default ('mysecretkey') is unsafe; fail fast if JWT_SECRET is missing. -const generateToken = (vigyId) => {
- return jwt.sign({ vigyId }, process.env.JWT_SECRET || 'mysecretkey', {
- expiresIn: '7d'
- });
-};
+const generateToken = (vigyId) => {
+ const secret = process.env.JWT_SECRET;
+ if (!secret) {
+ throw new Error('JWT_SECRET is not configured');
+ }
+ return jwt.sign({ vigyId }, secret, { expiresIn: '7d' });
+}; 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// @desc Register new Vigy user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// @route POST /api/vigy/register | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// @access Public | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const registerVigy = async (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fullname, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
email, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
password, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
phoneNumber, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
address, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
aadhaarNumber, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
gender, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dob, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bankAccountName, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bankAccountNumber, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bankName, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bankBranch, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ifscCode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
referralCode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
promotionalCode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} = req.body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+16
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Validate and normalize registration input up-front Add presence/type checks and sanitize to prevent malformed data and operator payloads. const registerVigy = async (req, res) => {
try {
const {
fullname,
email,
password,
phoneNumber,
address,
aadhaarNumber,
gender,
dob,
bankAccountName,
bankAccountNumber,
bankName,
bankBranch,
ifscCode,
referralCode,
promotionalCode
} = req.body;
+
+ // Basic type/format validation + normalization
+ const emailNorm = typeof email === 'string' ? email.trim().toLowerCase() : '';
+ const aadhaarNorm = typeof aadhaarNumber === 'string' ? aadhaarNumber.trim() : '';
+ if ([fullname, emailNorm, password, phoneNumber, address, aadhaarNorm, gender, dob].some(v => !v)) {
+ return res.status(400).json({ success: false, message: 'Missing required fields' });
+ }
+ if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailNorm)) {
+ return res.status(400).json({ success: false, message: 'Invalid email' });
+ }
+ if (!/^\d{12}$/.test(aadhaarNorm)) {
+ return res.status(400).json({ success: false, message: 'Invalid Aadhaar number' });
+ } 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Check if Vigy already exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const existingVigy = await Vigy.findOne({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$or: [{ email }, { aadhaarNumber }] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+35
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix NoSQL injection risk in uniqueness check Do not pass unvalidated values directly into the query. Use normalized strings. - const existingVigy = await Vigy.findOne({
- $or: [{ email }, { aadhaarNumber }]
- });
+ const existingVigy = await Vigy.findOne({
+ $or: [{ email: emailNorm }, { aadhaarNumber: aadhaarNorm }]
+ });
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (existingVigy) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return res.status(400).json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: existingVigy.email === email | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
? 'Email already registered' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
: 'Aadhaar number already registered' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Create new Vigy user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const vigy = new Vigy({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fullname, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
email, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
password, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
phoneNumber, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
address, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
aadhaarNumber, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
gender, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dob, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bankDetails: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
accountName: bankAccountName, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
accountNumber: bankAccountNumber, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bankName, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
branch: bankBranch, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ifscCode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
referralCode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
promotionalCode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+48
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use normalized values on create Ensure stored email/aadhaar are normalized to match queries and indexes. const vigy = new Vigy({
fullname,
- email,
+ email: emailNorm,
password,
phoneNumber,
address,
- aadhaarNumber,
+ aadhaarNumber: aadhaarNorm,
gender,
dob,
bankDetails: {
accountName: bankAccountName,
accountNumber: bankAccountNumber,
bankName,
branch: bankBranch,
ifscCode
},
referralCode,
promotionalCode
}); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await vigy.save(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Generate token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const token = generateToken(vigy._id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.status(201).json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Vigy registration successful', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
token, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
user: vigy.getPublicProfile() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.error('Vigy registration error:', error); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.status(500).json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Server error during registration', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error: error.message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+81
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Handle duplicate key (E11000) explicitly Avoid leaking internals and return deterministic 400s for unique constraint violations. } catch (error) {
- console.error('Vigy registration error:', error);
- res.status(500).json({
- success: false,
- message: 'Server error during registration',
- error: error.message
- });
+ if (error && error.code === 11000) {
+ const key = Object.keys(error.keyPattern || {})[0] || 'field';
+ const message =
+ key === 'email'
+ ? 'Email already registered'
+ : key === 'aadhaarNumber'
+ ? 'Aadhaar number already registered'
+ : 'Duplicate value';
+ return res.status(400).json({ success: false, message });
+ }
+ console.error('Vigy registration error:', error);
+ res.status(500).json({ success: false, message: 'Server error during registration' });
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// @desc Login Vigy user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// @route POST /api/vigy/login | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// @access Public | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const loginVigy = async (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { email, password } = req.body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Validate input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!email || !password) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return res.status(400).json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Please provide both email and password' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+96
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Harden login input to prevent NoSQL operator payloads Type-check and normalize before use. - // Validate input
- if (!email || !password) {
- return res.status(400).json({
- success: false,
- message: 'Please provide both email and password'
- });
- }
+ // Validate input + normalize
+ if (typeof email !== 'string' || typeof password !== 'string') {
+ return res.status(400).json({ success: false, message: 'Invalid payload' });
+ }
+ const emailNorm = email.trim().toLowerCase();
+ if (!emailNorm || !password) {
+ return res.status(400).json({ success: false, message: 'Please provide both email and password' });
+ } 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Find Vigy user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const vigy = await Vigy.findOne({ email }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+106
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use normalized email in the login query Avoid case-mismatch and ensure exact match. - const vigy = await Vigy.findOne({ email });
+ const vigy = await Vigy.findOne({ email: emailNorm }); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!vigy) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return res.status(401).json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Invalid credentials' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Check password | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const isPasswordValid = await vigy.comparePassword(password); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!isPasswordValid) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return res.status(401).json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Invalid credentials' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Check if account is approved | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (vigy.status !== 'approved' && vigy.status !== 'active') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return res.status(403).json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: `Account status: ${vigy.status}. Please wait for approval.` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Generate token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const token = generateToken(vigy._id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Login successful', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
token, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
user: vigy.getPublicProfile() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.error('Vigy login error:', error); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.status(500).json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Server error during login', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error: error.message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// @desc Get Vigy profile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// @route GET /api/vigy/profile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// @access Private | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const getVigyProfile = async (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const vigy = await Vigy.findById(req.vigyId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!vigy) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return res.status(404).json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Vigy user not found' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
user: vigy.getPublicProfile() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+154
to
+172
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainConfirm auth middleware contract for req.vigyId Ensure JWT auth middleware validates the token, handles expiry, and sets req.vigyId; return 401 if absent. 🏁 Script executed: #!/bin/bash
# Locate Vigy auth middleware and its contract
rg -nP -C2 'req\.vigyId|vigy.*auth|authenticateVigy|verifyVigy' --type=js Length of output: 4211 I requested the contents of 🏁 Script executed: #!/bin/bash
# List files in backend/middlewares
echo "Files in backend/middlewares:"
ls backend/middlewares
echo -e "\nSearch for jsonwebtoken import in middlewares:"
rg -nP "require\(['\"]jsonwebtoken['\"]\)" -g "backend/middlewares/*.js"
echo -e "\nSearch for req.vigyId assignment in middlewares:"
rg -nP "req\.vigyId" -g "backend/middlewares/*.js"
echo -e "\nSearch for auth middleware exports in middlewares:"
rg -nP "module\.exports.*auth" -g "backend/middlewares/*.js" Length of output: 639 🏁 Script executed: #!/bin/bash
# Print first 200 lines of the Passport middleware
sed -n '1,200p' backend/middlewares/Passport.js Length of output: 2408 🏁 Script executed: #!/bin/bash
# Locate and inspect the Vigy routes file for getVigyProfile
route_file=$(rg -l "getVigyProfile" -g "backend/routes/*.js")
echo "Route file: $route_file"
echo -e "\nRoute definition and applied middleware:"
sed -n '1,200p' "$route_file" Length of output: 257 🏁 Script executed: #!/bin/bash
# List top-level files and folders in backend
echo "backend directory listing:"
ls -1 backend Length of output: 234 🏁 Script executed: #!/bin/bash
# Search for jsonwebtoken usage across the codebase
rg -n "require.*jsonwebtoken" -n .
echo "Middlewares in backend/middlewares:"
ls -1 backend/middlewares
echo "Auth-related files in backend:"
find backend -type f -name "*auth*.js"
echo "Route files in backend/routes:"
ls -1 backend/routes
echo "Route files referencing vigyController:"
rg -l "vigyController" -g "backend/routes/*.js"
for f in $(rg -l "vigyController" -g "backend/routes/*.js"); do
echo "==== $f ===="
sed -n '1,200p' "$f"
done Length of output: 838 🏁 Script executed: #!/bin/bash
# Display vigyRoutes.js to inspect applied middleware
sed -n '1,200p' backend/routes/vigyRoutes.js Length of output: 3454 Implement and apply JWT auth middleware for Vigy routes
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.error('Get Vigy profile error:', error); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.status(500).json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Server error while fetching profile', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error: error.message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// @desc Update Vigy profile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// @route PUT /api/vigy/profile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// @access Private | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const updateVigyProfile = async (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const updates = req.body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
delete updates.password; // Don't allow password update through this route | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
delete updates.email; // Don't allow email update through this route | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
delete updates.aadhaarNumber; // Don't allow Aadhaar update through this route | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const vigy = await Vigy.findByIdAndUpdate( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
req.vigyId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ ...updates, updatedAt: Date.now() }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ new: true, runValidators: true } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+186
to
+197
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent mass-assignment in profile update Current spread allows hostile fields (e.g., role, status) to be updated. Whitelist allowed keys and rely on timestamps. - const updates = req.body;
- delete updates.password; // Don't allow password update through this route
- delete updates.email; // Don't allow email update through this route
- delete updates.aadhaarNumber; // Don't allow Aadhaar update through this route
-
- const vigy = await Vigy.findByIdAndUpdate(
- req.vigyId,
- { ...updates, updatedAt: Date.now() },
- { new: true, runValidators: true }
- );
+ const { password, email, aadhaarNumber, ...rest } = req.body;
+ const allowed = ['fullname', 'phoneNumber', 'address', 'gender', 'dob', 'bankDetails', 'referralCode', 'promotionalCode'];
+ const safeUpdates = {};
+ for (const key of allowed) {
+ if (Object.prototype.hasOwnProperty.call(rest, key)) {
+ safeUpdates[key] = rest[key];
+ }
+ }
+ const vigy = await Vigy.findByIdAndUpdate(
+ req.vigyId,
+ { $set: safeUpdates },
+ { new: true, runValidators: true }
+ ); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!vigy) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return res.status(404).json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Vigy user not found' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Profile updated successfully', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
user: vigy.getPublicProfile() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.error('Update Vigy profile error:', error); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.status(500).json({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
success: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Server error while updating profile', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error: error.message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
module.exports = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
registerVigy, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
loginVigy, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
getVigyProfile, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
updateVigyProfile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const mongoose = require('mongoose'); | ||
const Vigy = require('./models/Vigy'); | ||
const config = require('./config/config'); | ||
|
||
// Connect to MongoDB | ||
mongoose.connect(config.mongoURI) | ||
.then(() => console.log('Connected to MongoDB')) | ||
.catch(err => console.error('MongoDB connection error:', err)); | ||
|
||
// Test user data | ||
const testUser = { | ||
fullname: 'Test Vigy User', | ||
email: '[email protected]', | ||
password: 'test123', | ||
phoneNumber: '1234567890', | ||
address: '123 Test Street, Test City, Test State 123456', | ||
aadhaarNumber: '123456789012', | ||
gender: 'other', | ||
dob: '1990-01-01', | ||
bankDetails: { | ||
accountName: 'TEST USER', | ||
accountNumber: '1234567890', | ||
bankName: 'SBI', | ||
branch: 'TEST BRANCH', | ||
ifscCode: 'SBIN0001234' | ||
}, | ||
referralCode: 'TEST123', | ||
promotionalCode: 'PROMO123', | ||
status: 'approved', // Set to approved so user can login | ||
isVerified: true | ||
}; | ||
|
||
async function createTestUser() { | ||
try { | ||
// Check if user already exists | ||
const existingUser = await Vigy.findOne({ email: testUser.email }); | ||
|
||
if (existingUser) { | ||
console.log('Test user already exists'); | ||
console.log('Email:', existingUser.email); | ||
console.log('Status:', existingUser.status); | ||
return; | ||
} | ||
|
||
// Create new test user | ||
const vigy = new Vigy(testUser); | ||
await vigy.save(); | ||
|
||
console.log('Test user created successfully!'); | ||
console.log('Email:', vigy.email); | ||
console.log('Password: test123'); | ||
console.log('Status:', vigy.status); | ||
|
||
} catch (error) { | ||
console.error('Error creating test user:', error); | ||
} finally { | ||
mongoose.connection.close(); | ||
} | ||
} | ||
|
||
createTestUser(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Sessions over cross-site: set trust proxy and sameSite.
Without
app.set('trust proxy', 1)
andsameSite: 'none'
in production, secure cookies may be dropped in cross-site flows (e.g., OAuth).Also applies to: 26-30
🤖 Prompt for AI Agents