Skip to content
Merged
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
41 changes: 34 additions & 7 deletions apps/public-api/src/controllers/userAuth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ const buildSocialAuthUserPayload = async (usersColConfig, profile) => {
return buildAuthUserPayload(
usersColConfig,
{
email: profile.email,
email: String(profile.email).toLowerCase().trim(),
password: randomPassword,
username: profile.username,
name: profile.name,
Expand Down Expand Up @@ -577,7 +577,13 @@ const findOrCreateSocialUser = async ({ project, usersColConfig, Model, provider
throw err;
}

user = await Model.findOne({ email: profile.email });
const normalizedEmail = String(profile.email)
.toLowerCase()
.trim();

user = await Model.findOne({
email: normalizedEmail
});
if (user) {
const deletedMsg = checkUserSoftDeleted(user);
if (deletedMsg) {
Expand Down Expand Up @@ -1063,7 +1069,12 @@ module.exports.signup = async (req, res) => {

const newUserPayload = buildAuthUserPayload(
usersColConfig,
{ email, password, username, ...otherData },
{
email: normalizedEmail,
password,
username,
...otherData
},
hashedPassword,
false
);
Expand Down Expand Up @@ -1296,6 +1307,7 @@ module.exports.createAdminUser = async (req, res) => {

const parsedData = userSignupSchema.parse(req.body);
const { email, password, username, ...otherData } = parsedData;
const normalizedEmail = email.toLowerCase().trim();

// Get Mongoose Model
const usersColConfig = project.collections.find(c => c.name === 'users');
Expand All @@ -1304,7 +1316,9 @@ module.exports.createAdminUser = async (req, res) => {
const connection = await getConnection(project._id);
const Model = getCompiledModel(connection, usersColConfig, project._id, project.resources.db.isExternal);

const existingUser = await Model.findOne({ email });
const existingUser = await Model.findOne({
email: normalizedEmail
});
if (existingUser) {
return res.status(400).json({ error: "User already exists with this email." });
}
Expand All @@ -1314,16 +1328,29 @@ module.exports.createAdminUser = async (req, res) => {

const newUserPayload = buildAuthUserPayload(
usersColConfig,
{ email, password, username, ...otherData },
{
email: normalizedEmail,
password,
username,
...otherData
},
hashedPassword,
true
);

const result = await Model.create(newUserPayload);

res.status(201).json({
message: "User created successfully",
user: { _id: result._id, email, username, createdAt: newUserPayload.createdAt }
success: true,
data: {
user: {
_id: result._id,
email: normalizedEmail,
username,
createdAt: newUserPayload.createdAt
}
},
message: "User created successfully"
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
yash-pouranik marked this conversation as resolved.

} catch (err) {
Expand Down
Loading