1
0

Video 113: Authenticating User Endpoints

This commit is contained in:
JayWll 2020-07-15 16:26:27 -06:00
parent db05569296
commit b06a1d3e88

View File

@ -53,23 +53,7 @@ router.get('/users/me', auth, async (req, res) => {
res.send(req.user) res.send(req.user)
}) })
router.get('/users/:id', async (req, res) => { router.patch('/users/me', auth, async (req, res) => {
const _id = req.params.id
try {
const user = await User.findById(_id)
if (!user) {
return res.status(404).send()
}
res.send(user)
} catch (e) {
res.status(500).send()
}
})
router.patch('/users/:id', async (req, res) => {
const updates = Object.keys(req.body) const updates = Object.keys(req.body)
const allowedUpdates = ['name', 'email', 'password', 'age'] const allowedUpdates = ['name', 'email', 'password', 'age']
const isValidOperation = updates.every((update) => allowedUpdates.includes(update)) const isValidOperation = updates.every((update) => allowedUpdates.includes(update))
@ -79,30 +63,18 @@ router.patch('/users/:id', async (req, res) => {
} }
try { try {
const user = await User.findById(req.params.id) updates.forEach((update) => req.user[update] = req.body[update])
await req.user.save()
updates.forEach((update) => user[update] = req.body[update]) res.send(req.user)
await user.save()
if (!user) {
return res.status(404).send()
}
res.send(user)
} catch (e) { } catch (e) {
res.status(400).send(e) res.status(400).send(e)
} }
}) })
router.delete('/users/:id', async (req, res) => { router.delete('/users/me', auth, async (req, res) => {
try { try {
const user = await User.findByIdAndDelete(req.params.id) await req.user.remove()
res.send(req.user)
if (!user) {
return res.status(404).send()
}
res.send(user)
} catch (e) { } catch (e) {
res.status(500).send() res.status(500).send()
} }