(req, res, next)
| 23 | }; |
| 24 | |
| 25 | export const authenticate = (req, res, next) => { |
| 26 | const authHeader = req.get('authorization'); |
| 27 | if (!authHeader) { |
| 28 | return res.sendStatus(401); |
| 29 | } |
| 30 | |
| 31 | const token = authHeader.split(' ')[1]; |
| 32 | const decoded = jwt.verify(token, process.env.JWT_SECRET); |
| 33 | models.User.findOne({where: {id: decoded.id}}) |
| 34 | .then(user => { |
| 35 | req.user = user; |
| 36 | next(); |
| 37 | }) |
| 38 | .catch(() => res.sendStatus(400)); |
| 39 | }; |