From 83941c59d5a7c7e5af140c6623bc3505321dffe3 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Thu, 23 Sep 2021 22:43:45 -0700 Subject: [PATCH] return error when register user with used email: --- index.js | 13 ++++++------- mongoDB.js | 6 +++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 4b0beb0..e008853 100644 --- a/index.js +++ b/index.js @@ -76,21 +76,21 @@ DB.getDB.then((DB)=>{ if(!username || !password || !email) return res.json({status: "fail"}) //const hashedPassword = await bcrypt.hash(password, 10); const hashedPassword = await bcrypt.hash(password, 10); - const success = await DB.newUser({ - username: username, - email: email, + const response = await DB.newUser({ + username: username.toLowerCase(), + email: email.toLowerCase(), password: hashedPassword }); - if(success){ + if(!response.toLowerCase){ let user = { - userid: success.insertedId, + userid: response.insertedId, profile: profile, } const userObj = new Profile(user); await DB.newProfile(userObj); return await login(req, res); } - res.redirect('/signup'); + return res.json({status: response}) } app.route('/signup').get(async (req, res) => { return await signup(req, res); @@ -125,7 +125,6 @@ DB.getDB.then((DB)=>{ res.cookie('session_id', doc.insertedId, cookiesOptions); //Chooses the most recent update profile const latestProfile = await DB.latestProfile(user._id); - console.log("latestProfile", latestProfile) res.cookie('profile_id', latestProfile._id, cookiesOptions); return res.json({ status: "ok", diff --git a/mongoDB.js b/mongoDB.js index 3f0969d..807c929 100644 --- a/mongoDB.js +++ b/mongoDB.js @@ -50,8 +50,12 @@ const getDB = new Promise((resolve, reject) => { DB.newUser = (userInformation)=>{ return DB.usersCol.insertOne(userInformation).catch((err)=>{ + if (err.name === 'MongoError' && err.code === 11000) { + // Duplicate username + return "User already exists"; + } console.log(err); - return false; + return "System Error"; }); };