text: const express = require('express');
  const router = express.Router();
  const bcrypt = require('bcryptjs');
  const { check, validationResult } = require('express-validator/check');
  const jwt = require('jsonwebtoken');
  const config = require('config');
  const auth = require('../middleware/auth')
  const User = require('../models/User')
  
  
  //anytime i have a route that is private (or protect a route)i need to bring in middleware
  // @route GET api/auth
  // @description Get logged in user
  // @access Private
  router.get('/', auth, async (req, res) =>{
      try {
          const user = await User.findById(req.user.id).select('-password');
          res.json(user);
      } catch (err) {
          console.error(err.message);
          res.status(500).send('Server error')
          
      }
  });
  
  // @route POST api/auth
  // @description Auth user & get token
  // @access Public
  router.post('/', 
  [check('email', 'Please include a valid email').isEmail(),
  check('password','Password is required').exists()
  ],
  async (req, res) =>{
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
          return res.status(400).json({ errors: errors.array() });
      }
      const { email, password } = req.body;
  
      
      try {
         let user = await User.findOne({ email });
  
         if(!user) {
             return res.status(400).json({ msg: 'User already exists' });
  
         }
         const isMatch = await bcrypt.compare(password, user.password);
  
         if(!isMatch) {
             return res.status(400).json({ msg:'Invalid Credentials' })
         }
  
         const payload = {
          user: {
              id: user.id
  
          }
      }
      jwt.sign(payload, config.get('jwtSecret'), {
          expiresIn: 360000
      }, (err, token) => {
          if (err) throw err;
          res.json({ token });
      })
  
      } catch (error) {
          console.error(err.message);
          res.status(500).send('server error')
      }
  });
  
  
  module.exports = router;