text: const { validationResult} = require('express-validator');
  const loginService = require('../services/loginService');
  
  let getLoginPage = (req, res) => {
      return res.render('login', {
          title: 'Login',
          errors: req.flash('errors')
      });
  }
  
  let handleLogin = async (req, res) => {
      let errorsArr = [];
      let validationErrors = validationResult(req);
      if (!validationErrors.isEmpty()) {
          let errors = Object.values(validationErrors.mapped());
          errors.forEach((item) => {
              errorsArr.push(item.msg);
          });
          req.flash("errors", errorsArr);
          return res.redirect("/login");
      }
  
      try {
          await loginService.handleLogin(req.body.email, req.body.password);
          return res.redirect("/");
      } catch (err) {
          req.flash("errors", err);
          return res.redirect("/login");
      }
  };
  
  let checkLoggedIn = (req, res, next) => {
      // console.log("READ.USER...", req.user);
      if (!req.isAuthenticated()) {
          return res.redirect("/login");
      }
      next();
  };
  
  let checkLoggedOut = (req, res, next) => {
      
      if (req.isAuthenticated()) {
          return res.redirect("/");
      }
      next();
  };
  
  let postLogOut = (req, res) => {
      req.session.destroy(function(err) {
          return res.redirect("/login");
      });
  };
  
  module.exports = {
      getLoginPage: getLoginPage,
      handleLogin: handleLogin,
      checkLoggedIn: checkLoggedIn,
      checkLoggedOut: checkLoggedOut,
      postLogOut: postLogOut
  };
  