@@ -939,6 +939,7 @@ module.exports.signup = async (req, res) => {
939939 const project = req . project ;
940940
941941 const { email, password, username, ...otherData } = userSignupSchema . parse ( req . body ) ;
942+ const normalizedEmail = email . toLowerCase ( ) . trim ( ) ;
942943
943944 // Get Mongoose Model
944945 const usersColConfig = project . collections . find ( c => c . name === 'users' ) ;
@@ -947,7 +948,8 @@ module.exports.signup = async (req, res) => {
947948 const connection = await getConnection ( project . _id ) ;
948949 const Model = getCompiledModel ( connection , usersColConfig , project . _id , project . resources . db . isExternal ) ;
949950
950- const existingUser = await Model . findOne ( { email } ) ;
951+ const existingUser = await Model . findOne ( { email : normalizedEmail } ) ;
952+
951953 if ( existingUser ) {
952954 // Check if user is unverified. If so, we can trigger a resend instead of a hard error.
953955 const verificationField = getVerificationField ( usersColConfig ) ;
@@ -1040,14 +1042,16 @@ module.exports.login = async (req, res) => {
10401042 try {
10411043 const project = req . project ;
10421044 const { email, password } = loginSchema . parse ( req . body ) ;
1045+ const normalizedEmail = email . toLowerCase ( ) . trim ( ) ;
10431046
10441047 const usersColConfig = project . collections . find ( c => c . name === 'users' ) ;
10451048 if ( ! usersColConfig ) return res . status ( 404 ) . json ( { error : "Auth collection not found" } ) ;
10461049
10471050 const connection = await getConnection ( project . _id ) ;
10481051 const Model = getCompiledModel ( connection , usersColConfig , project . _id , project . resources . db . isExternal ) ;
10491052
1050- const user = await Model . findOne ( { email } ) ;
1053+ const user = await Model . findOne ( { email : normalizedEmail } ) ;
1054+
10511055 if ( ! user ) return res . status ( 400 ) . json ( { error : "Invalid email or password" } ) ;
10521056
10531057 const validPass = await bcrypt . compare ( password , user . password ) ;
@@ -1222,11 +1226,13 @@ module.exports.verifyEmail = async (req, res) => {
12221226 try {
12231227 const project = req . project ;
12241228 const { email, otp } = verifyOtpSchema . parse ( req . body ) ;
1229+ const normalizedEmail = email . toLowerCase ( ) . trim ( ) ;
12251230
1226- const redisKey = `project:${ project . _id } :otp:verification:${ email } ` ;
1231+ const redisKey = `project:${ project . _id } :otp:verification:${ normalizedEmail } ` ;
12271232 const storedOtp = await redis . get ( redisKey ) ;
12281233
12291234 if ( ! storedOtp || storedOtp !== otp ) {
1235+
12301236 return res . status ( 400 ) . json ( { error : "Invalid or expired OTP" } ) ;
12311237 }
12321238
@@ -1261,11 +1267,13 @@ module.exports.resendVerificationOtp = async (req, res) => {
12611267 try {
12621268 const project = req . project ;
12631269 const { email } = onlyEmailSchema . parse ( req . body ) ;
1270+ const normalizedEmail = email . toLowerCase ( ) . trim ( ) ;
12641271
12651272 const { usersColConfig, Model } = await getUsersModel ( project ) ;
12661273 if ( ! Model ) return res . status ( 404 ) . json ( { error : "Auth collection not found" } ) ;
12671274
1268- const user = await Model . findOne ( { email } ) ;
1275+ const user = await Model . findOne ( { email : normalizedEmail } ) ;
1276+
12691277 if ( ! user ) {
12701278 return res . status ( 400 ) . json ( { error : "User not found with this email." } ) ;
12711279 }
0 commit comments