From 0ca589cbe875f2282f6cd8612511e96280763968 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Sun, 2 Feb 2025 23:55:58 -0500 Subject: [PATCH] Catching werid cornercase on which users don't have profiles. --- index.js | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index c83a2bc..aa92988 100644 --- a/index.js +++ b/index.js @@ -175,20 +175,26 @@ DB.getDB.then((DB) => { // TODO: Also add salt parameter here. const isSamePassword = await bcrypt.compare(password, user.password); if (!isSamePassword) return res.json({ status: "incorrect password" }); - // Store a new session loging on DB, and use ID as session ID - const sessionObj = await DB.newSession(user._id); - // Create coockies with information for Auth - res.cookie('user_sid', user._id, cookiesOptions); - res.cookie('session_id', sessionObj.insertedId, cookiesOptions); - // Chooses the most recent update profile as current active profile - const latestUpdatedProfile = await DB.latestProfile(user._id); - res.cookie('profile_id', latestUpdatedProfile._id, cookiesOptions); - return res.json({ - status: "ok", - user_sid: user._id, - session_id: sessionObj.insertedId, - profile_id: latestUpdatedProfile._id - }); + try { + // Store a new session loging on DB, and use ID as session ID + const sessionObj = await DB.newSession(user._id); + // Create coockies with information for Auth + res.cookie('user_sid', user._id, cookiesOptions); + res.cookie('session_id', sessionObj.insertedId, cookiesOptions); + // Chooses the most recent update profile as current active profile + const latestUpdatedProfile = await DB.latestProfile(user._id); + res.cookie('profile_id', latestUpdatedProfile._id, cookiesOptions); + return res.json({ + status: "ok", + user_sid: user._id, + session_id: sessionObj.insertedId, + profile_id: latestUpdatedProfile._id + }); + } catch (error) { + console.error(error); + return res.json({ status: "Error on this User Profile, please contact admin." }); + } + } app.route('/login').get(async (req, res) => { return await login(req, res);