text: const jwt = require('jsonwebtoken');
  const User = require('../models/user');
  
  const auth = async (req, res, next) => {
    try {
      const token = req.header('Authorization').replace('Bearer ', '');
      const decoded = jwt.verify(token, process.env.JWT_SECRET);
  
      // verrify contains { _id: '61ebcd9ecb7c0ca1b53098ac', iat: 1642843550 }
      const user = await User.findOne({
        _id: decoded._id,
        'tokens.token': token,
      });
  
      if (!user) {
        throw new Error();
      }
  
      req.token = token;
      req.user = user;
  
      next();
    } catch (e) {
      res.status(401).send({ error: 'Please Authenticate' });
    }
  };
  
  module.exports = auth;
  