Video 111: Logging Out
This commit is contained in:
parent
b41ad999ee
commit
bae66dbf89
@ -11,6 +11,7 @@ const auth = async (req, res, next) => {
|
||||
throw new Error()
|
||||
}
|
||||
|
||||
req.token = token
|
||||
req.user = user
|
||||
next()
|
||||
} catch (e) {
|
||||
|
@ -25,6 +25,30 @@ router.post('/users/login', async (req, res) => {
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/users/logout', auth, async (req, res) => {
|
||||
try {
|
||||
req.user.tokens = req.user.tokens.filter((token) => {
|
||||
return token.token !== req.token
|
||||
})
|
||||
await req.user.save()
|
||||
|
||||
res.send()
|
||||
} catch(e) {
|
||||
res.status(500).send()
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/users/logoutAll', auth, async (req, res) => {
|
||||
try {
|
||||
req.user.tokens = []
|
||||
await req.user.save()
|
||||
|
||||
res.send()
|
||||
} catch(e) {
|
||||
res.status(500).send()
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/users/me', auth, async (req, res) => {
|
||||
res.send(req.user)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user