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
4 changes: 2 additions & 2 deletions backend/src/Utils/Validetor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import validator from "validator"
const Validate = (data) => {
const { name, email, password } = data;

if (!name || !email || !password) {
throw new Error("Some Field Missing");
if (typeof name !== "string" || typeof email !== "string" || typeof password !== "string") {
throw new Error("Invalid field type");
}

// Optional: Email validation
Expand Down
12 changes: 10 additions & 2 deletions backend/src/controllers/userAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ export const register = async (req, res) => {
Validate(req.body);
const { name, email, password } = req.body;

const existingUser = await User.findOne({ email: String(email) });
if (existingUser) {
return res.status(400).json({
success: false,
error: "User already exists. Please login instead.",
});
}

const hashedPassword = await bcrypt.hash(password, 10);

const user = await User.create({
Expand Down Expand Up @@ -52,11 +60,11 @@ export const login = async (req, res) => {
try {
const { email, password } = req.body;

if (!email || !password) {
if (typeof email !== "string" || typeof password !== "string") {
throw new Error("Invalid Credentials");
}

const user = await User.findOne({ email });
const user = await User.findOne({ email: String(email) });

if (!user) throw new Error("User not found");

Expand Down
Loading