Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions backend/controllers/userAuth.controller.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
const { z } = require('zod');
const mongoose = require('mongoose');
const { loginSchema } = require('../utils/input.validation');
const { loginSchema, signupSchema } = require('../utils/input.validation');
Comment thread
coderabbitai[bot] marked this conversation as resolved.

module.exports.signup = async (req, res) => {
try {
const project = req.project;

// Zod Validation (Prevents NoSQL Injection too)
const { email, password, ...otherData } = loginSchema.parse(req.body);
const { email, password, username, ...otherData } = signupSchema.parse(req.body);

const collectionName = `${project._id}_users`;
const collection = mongoose.connection.db.collection(collectionName);
Expand All @@ -22,9 +23,9 @@ module.exports.signup = async (req, res) => {
const hashedPassword = await bcrypt.hash(password, salt);

const newUser = {
username,
email,
password: hashedPassword,
...otherData,
createdAt: new Date()
};

Expand Down
4 changes: 2 additions & 2 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions backend/utils/input.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ module.exports.loginSchema = z.object({
.max(100, { message: "Password is too long." })
});

module.exports.signupSchema = z.object({
username: z.string()
.min(3, { message: "Username must be at least 3 characters." })
.max(30, { message: "Username is too long." }),

email: z.string()
.min(1, { message: "Email is required." })
.email({ message: "Invalid email format." })
.max(100, { message: "Email is too long." }),

password: z.string()
.min(6, { message: "Password must be at least 6 characters." })
.max(100, { message: "Password is too long." })
});

module.exports.changePasswordSchema = z.object({
currentPassword: z.string().min(1, "Current password is required"),
newPassword: z.string().min(6, "New password must be at least 6 characters")
Expand Down
Loading