From 3e4f772ce0fec0a4ffc2645c8e6c628a37b85182 Mon Sep 17 00:00:00 2001 From: qlam2001 <90201171+qlam2001@users.noreply.github.com> Date: Sat, 27 Jul 2024 23:23:18 -0400 Subject: [PATCH 1/3] Create userFunctions.js 1) remove all logics from the route to this file 2) add feature hash password and verify login P/s: you should install bcrypt to run smoothly via npm install bcrypt --- server/database/userFunctions.js | 67 ++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 server/database/userFunctions.js diff --git a/server/database/userFunctions.js b/server/database/userFunctions.js new file mode 100644 index 0000000..c27a8c6 --- /dev/null +++ b/server/database/userFunctions.js @@ -0,0 +1,67 @@ +const User = require('../models/userSchema'); +const bcrypt = require('bcrypt') + +async function findUsers(req,res){ + try { + const users = await User.find(); + res.json(users); + } catch (err) { + res.status(500).json({ message: err.message }); + console.log("users unsuccessfully sent") + //test + + } +} +async function createNewUser(req,res){ + const user = new User({ + firstName: req.body.firstName, + lastName: req.body.lastName, + email: req.body.email, + school: req.body.selectedSchool, + password: await hashPassWord(req.body.password), + role: req.body.role + }); + + try { + const newUser = await user.save(); + res.status(201).json(newUser); + console.log("created new user") + } catch (err) { + res.status(400).json({ message: err.message }); + console.log("error creating new user") + } + +} +async function deleteUser(req,res){ + try { + const email = req.params.email; + const user = await User.findOne({ email: email }); + if (user === null) { + return res.status(404).json({ message: "User not found" }); + } + await User.deleteOne({ email: email }); + res.json({ message: "Deleted User" }); + } catch (err) { + res.status(500).json({ message: `Internal server error: ${err.message}` }); + } +} +async function hashPassWord(plainPass){ + const salt = await bcrypt.genSalt() + const modifiedPass = await bcrypt.hash(plainPass,salt) + return modifiedPass +} +async function verifyLogin(req,res){ + const email = req.body.email + const password = req.body.password + let confirm = false + await User.exists({"email":email}).exec().then(async e =>{ + if(e){ + const user = (await User.find({"_id":e}))[0] + await bcrypt.compare(password,user.password) ? confirm = true: confirm = false + } + }) + return confirm + +} + +module.exports = {findUsers,createNewUser,deleteUser, verifyLogin} From 40e4e8044e2a3ef140c109eefe314dad8af44415 Mon Sep 17 00:00:00 2001 From: qlam2001 <90201171+qlam2001@users.noreply.github.com> Date: Sat, 27 Jul 2024 23:24:02 -0400 Subject: [PATCH 2/3] Update users.js modified route --- server/routes/users.js | 49 ++++++------------------------------------ 1 file changed, 7 insertions(+), 42 deletions(-) diff --git a/server/routes/users.js b/server/routes/users.js index 2954af0..03ba759 100644 --- a/server/routes/users.js +++ b/server/routes/users.js @@ -1,55 +1,20 @@ +const {findUsers,createNewUser,deleteUser, verifyLogin} = require('../database/userFunctions') const express = require('express'); -const User = require('../models/userSchema'); const router = express.Router(); router.get('/', async (req, res) => { - try { - const users = await User.find(); - res.json(users); - } catch (err) { - res.status(500).json({ message: err.message }); - console.log("users unsuccessfully sent") - //test - - } + await findUsers(req,res) }); router.post('/', async (req, res) => { - console.log(req.body) - const user = new User({ - firstName: req.body.firstName, - lastName: req.body.lastName, - email: req.body.email, - school: req.body.school, - role: req.body.role, - password: req.body.password - }); - - try { - const newUser = await user.save(); - res.status(201).json(newUser); - console.log("created new user") - } catch (err) { - res.status(400).json({ message: err.message }); - console.log("error creating new user") - } + await createNewUser(req,res) }); router.delete('/email/:email', async (req, res) => { - try { - const email = req.params.email; - const user = await User.findOne({ email: email }); - if (user === null) { - return res.status(404).json({ message: "User not found" }); - } - await User.deleteOne({ email: email }); - console.log("deleted user", email) - res.json({ message: "Deleted User" }); - } catch (err) { - console.log("error deleting user") - - res.status(500).json({ message: `Internal server error: ${err.message}` }); - } + await deleteUser(req,res) }); +router.get('/login', async(req,res,next) =>{ + res.json(await verifyLogin(req,res,next)) +}) module.exports = router; From 872100ac3861bbec361bcaaa2a52f3124b97c475 Mon Sep 17 00:00:00 2001 From: qlam2001 <90201171+qlam2001@users.noreply.github.com> Date: Sat, 27 Jul 2024 23:29:28 -0400 Subject: [PATCH 3/3] Update userFunctions.js --- server/database/userFunctions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/database/userFunctions.js b/server/database/userFunctions.js index c27a8c6..b820c7d 100644 --- a/server/database/userFunctions.js +++ b/server/database/userFunctions.js @@ -57,7 +57,7 @@ async function verifyLogin(req,res){ await User.exists({"email":email}).exec().then(async e =>{ if(e){ const user = (await User.find({"_id":e}))[0] - await bcrypt.compare(password,user.password) ? confirm = true: confirm = false + confirm = await bcrypt.compare(password,user.password) ? true:false } }) return confirm