-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from musicApprentice/Quoc-Lam-branch-No1
Quoc lam branch no1
- Loading branch information
Showing
2 changed files
with
74 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |