text: // data to create new user or login with auth
  const router = require('express').Router();
  const { User } = require('../../models');
  
  // create new user
  router.post('/', async (req, res) => {
    try {
      const userData = await User.create(req.body);
  
      req.session.save(() => {
        req.session.user_id = userData.id;
        req.session.logged_in = true;
  
        res.status(200).json({ user: userData, message: 'A newcomer! Take a look around and get comfortable!' });
      });
    } catch (err) {
      res.status(400).json(err);
    }
  });
  
  // login with credentials
  router.post('/login', async (req, res) => {
    try {
      const userData = await User.findOne({ where: { email: req.body.email} });
  
      if (!userData) {
        res
          .status(400)
          .json({ message: 'Oops! Double check your credentials and try again. Email' });
        return;
      }
  
      const validPassword = await userData.checkPassword(req.body.password);
  
      if (!validPassword) {
        res
          .status(400)
          .json({ message: 'Oops! Double check your credentials and try again. Password' });
        return;
      }
  
      req.session.save(() => {
        req.session.user_id = userData.id;
        req.session.logged_in = true;
        
        res.json({ user: userData, message: 'Hey hey, welcome back!' });
      });
  
    } catch (err) {
      res.status(400).json(err);
    }
  });
  
  router.post('/logout', (req, res) => {
    if (req.session.logged_in) {
      req.session.destroy(() => {
        res.status(204).end();
      });
    } else {
      res.status(404).end();
    }
  });
  
  module.exports = router;
  