text: const express = require('express');
  const router = express.Router();
  const bcrypt = require('bcryptjs');
  const auth = require('../../middleware/auth');
  const jwt = require('jsonwebtoken');
  const config = require('config');
  const { check, validationResult } = require('express-validator');
  
  const User = require('../../models/User');
  
  
  // GET MY PROFILE : for creating loadUser()
  
  router.get('/', auth,  async (req, res) => {
  
      //At this point, thus when the auth middleware extract the user from,
      //from the token, the user now is available in the request body as,
      //req.user and when we want the id we will do req.user.id.
      //Now we have to find this id in our database so that we display,
      //that user profile
      
    try {
      const user = await User.findById(req.user.id).select('-password');
      console.log(req)
      res.json(user);
    } catch (err) {
      console.error(err);
      res.status(500).send('Server Errors');
    }
  });
  
  //==============
  //=======login
  //=================
  
  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({ errors: [{ msg: 'Invalid Credentials' }] });
        }
  
        const isMatch = await bcrypt.compare(password, user.password);
  
        if (!isMatch) {
          return res
            .status(400)
            .json({ errors: [{ 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 (err) {
        console.error(err.message);
        res.status(500).send('Server error');
      }
    }
  );
  
  
  
  
  
  module.exports = router;
  