Skip to content

Commit

Permalink
Merge pull request #20 from musicApprentice/Quoc-Lam-branch-No1
Browse files Browse the repository at this point in the history
Quoc lam branch no1
  • Loading branch information
musicApprentice authored Aug 2, 2024
2 parents c4a7d71 + 872100a commit ecc3a63
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 42 deletions.
67 changes: 67 additions & 0 deletions server/database/userFunctions.js
Original file line number Diff line number Diff line change
@@ -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]
confirm = await bcrypt.compare(password,user.password) ? true:false
}
})
return confirm

}

module.exports = {findUsers,createNewUser,deleteUser, verifyLogin}
49 changes: 7 additions & 42 deletions server/routes/users.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit ecc3a63

Please sign in to comment.