existing users email restricted

This commit is contained in:
unknown 2026-01-19 09:44:28 +05:30
parent b48c422b63
commit ff78e28e6f
2 changed files with 38 additions and 11 deletions

View File

@ -6,6 +6,7 @@ const crypto = require("crypto");
const { sendEmailService } = require("../services/email.service");
const { sanitizeForLog } = require("../utils/sanitize");
const logger = require("../services/logger");
const User = db.user;
// Create User
exports.createUser = async (req, res) => {
@ -35,6 +36,26 @@ exports.createUser = async (req, res) => {
});
}
email = email.trim().toLowerCase();
const existingUser = await EstablishmentUser.findOne({
where: { email, is_active: true }
});
if (existingUser) {
return res.status(409).json({
status: "error",
message: "Email already exists"
});
}
const existingAdminUser = await User.findOne({ where: { email, is_active: true } });
if (existingAdminUser) {
logger.warn(`Email already exists in Admin Users: ${email}`);
return res.status(409).send({
status: "failed",
message: "This email address is already registered in the admin users."
});
}
if (typeof password !== "string" || password.length < 6) {
return res.status(400).json({
@ -56,17 +77,6 @@ exports.createUser = async (req, res) => {
gender = gender.charAt(0).toUpperCase() + gender.slice(1);
}
const existingUser = await EstablishmentUser.findOne({
where: { email, is_active: true }
});
if (existingUser) {
return res.status(409).json({
status: "error",
message: "Email already exists"
});
}
// Store plain password for email before hashing
const plainPasswordForEmail = password;
const hashedPassword = await bcrypt.hash(password, 10);

View File

@ -5,12 +5,29 @@ const logger = require("../services/logger");
require("dotenv").config();
const User = db.user;
const EstablishmentUser = db.EstablishmentUser;
// Create User
exports.createUser = async (req, res) => {
try {
const { name, email, password } = req.body;
const existingUser = await User.findOne({ where: { email, is_active: true } });
const establishmentExist = await EstablishmentUser.findOne({ where: { email, is_active: true }});
if (existingUser) {
logger.warn(`Email already exists: ${email}`);
return res.status(409).send({
status: "failed",
message: "Email already exists"
});
}
if (establishmentExist) {
logger.warn(`Email already exists in Company Profile: ${email}`);
return res.status(409).send({
status: "failed",
message: "This email address is already registered in the company profile."
});
}
const hashedPassword = await bcrypt.hash(password, 10);
const user = await User.create({ name, email, password: hashedPassword });