From f024465afdde2cb52973ebb07c38c301fb98aad5 Mon Sep 17 00:00:00 2001 From: tomivm Date: Sat, 22 Jul 2023 08:36:21 -0300 Subject: [PATCH 1/2] extract a function to fill user data on login --- api/controllers/user.js | 55 ++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/api/controllers/user.js b/api/controllers/user.js index 0e6c098e..8a238505 100644 --- a/api/controllers/user.js +++ b/api/controllers/user.js @@ -384,6 +384,35 @@ function updateUser(req, res) { }); } +const populateUserForLogin = async (req, user, email) => { + const userId = user._id; + req.session.userId = userId; + + const tokenString = auth.issueToken({ + email, + id: userId + }); + + if (!user.location || !user.location.country) + try { + await updateUserLocation(req.ip, user); + } catch (error) { + console.error(error.message); + } + + const settings = await getSettings(user); + const subscriber = await getSubscriber(user); + + const response = { + ...user.toJSON(), + settings, + subscriber, + birthdate: moment(user.birthdate).format('YYYY-MM-DD'), + authToken: tokenString + }; + return response; +}; + function loginUser(req, res) { const { email, password } = req.body; @@ -393,31 +422,7 @@ function loginUser(req, res) { message: 'Wrong email or password.' }); } else { - const userId = user._id; - req.session.userId = userId; - - const tokenString = auth.issueToken({ - email, - id: userId - }); - - if (!user.location || !user.location.country) - try { - await updateUserLocation(req.ip, user); - } catch (error) { - console.error(error.message); - } - - const settings = await getSettings(user); - const subscriber = await getSubscriber(user); - - const response = { - ...user.toJSON(), - settings, - subscriber, - birthdate: moment(user.birthdate).format('YYYY-MM-DD'), - authToken: tokenString - }; + const response = await populateUserForLogin(req, user, email); return res.status(200).json(response); } }); From 24f3a03e5fbd3fe952f808d7c14d8ac8515eba74 Mon Sep 17 00:00:00 2001 From: tomivm Date: Sat, 22 Jul 2023 08:39:52 -0300 Subject: [PATCH 2/2] Refactor Activate user to send user data after activation of an account --- api/controllers/user.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/api/controllers/user.js b/api/controllers/user.js index 8a238505..bbc69105 100644 --- a/api/controllers/user.js +++ b/api/controllers/user.js @@ -234,27 +234,32 @@ async function createOrUpdateUser(accessToken, profile, type = 'facebook') { function activateUser(req, res) { const url = req.swagger.params.url.value; - nev.confirmTempUser(url, function (err, user) { + const confirmTempUserCallback = async (err, user) => { if (user) { - nev.sendConfirmationEmail(user.email, function (err, info) { + nev.sendConfirmationEmail(user.email, async function(err, info) { if (err) { - return res.status(404).json({ - message: 'ERROR: sending confirmation email FAILED ' + info - }); + console.error( + 'message: ERROR: sending confirmation email FAILED' + info + ); } - return res.status(200).json({ - success: 1, - userid: user._id, - message: 'CONFIRMED!' - }); + }); + const response = await populateUserForLogin(req, user, user.email); + return res.status(200).json({ + ...response, + success: 1, + userid: user._id, + message: 'CONFIRMED!' }); } else { return res.status(404).json({ - message: 'ERROR: confirming your temporary user FAILED, please try to login again', - error: 'ERROR: confirming your temporary user FAILED, please try to login again' + message: + 'ERROR: confirming your temporary user FAILED, please try to login again', + error: + 'ERROR: confirming your temporary user FAILED, please try to login again' }); } - }); + }; + nev.confirmTempUser(url, confirmTempUserCallback); } async function listUser(req, res) {