text: const jwt = require('jsonwebtoken');
  // const config = require('config');
  require('dotenv').config();
  const JWT_SECRET = process.env.JWT_SECRET;
  
  module.exports = (req, res, next) => {
    // Get token from header
    const token = req.header('x-auth-token');
  
    // Check if token exists
    if (!token) {
      return res.status(401).json({ msg: 'No token, authorization denied' });
    }
  
    // Verify token
    try {
      const decoded = jwt.verify(token, JWT_SECRET);
  
      // The payload sent in /api/users and /api/auth
      // payload = { user: { id: user.id } }
      // req.user can now access that id with req.user.id
      req.user = decoded.user;
      next();
    } catch (err) {
      res.status(401).json({ msg: 'Token is not valid' });
    }
  }