auth.js 463 B

12345678910111213141516171819
  1. const jwt = require('jsonwebtoken')
  2. module.exports = (req, res, next) => {
  3. if (req.method === 'OPTIONS') {
  4. return next()
  5. }
  6. try {
  7. const token = req.headers.authorization.split(' ')[1]
  8. if (!token) {
  9. return res.status(401).json({ message: 'no autorization' })
  10. }
  11. const decod = jwt.verify(token, config.get('jwtSecret'))
  12. req.user = decoded
  13. next()
  14. } catch (e) {
  15. res.status(401).json({ message: 'no autorization' })
  16. }
  17. }