I am trying to do my own site (express, html, css) with authentication but can't figure out how to set and receive tokens from headers. So far, in the tutorial I have followed I manually set the bearer tokens in Postman and received it like this
const auth = async (req, res, next) => {
try {
const token = req.header('Authorization').replace('Bearer ', '')
const decoded = jwt.verify(token, 'thisismynewcourse')
const user = await User.findOne({
_id: decoded._id,
'tokens.token': token
})
if (!user) {
throw new Error('No user')
}
req.token = token
req.user = user
next()
} catch (e) {
res.status(401).send({error: 'Unable to authenticate'})
}
}
How would I achieve this without Postman?