albertosuarez8 / Brewery-Reviews

2 stars 1 forks source link

User Routes #6

Closed albertosuarez8 closed 1 year ago

laps22 commented 1 year ago

Controller folder Api UserRoutse.js

jujubea22 commented 1 year ago

User routes: const router = require('express').Router(); const { User } = require('models');

router.post('/login', async (req, res) => { try { const userData = await User.findOne({ where: { username: text } });

if (!userData) {
  res
    .status(400)
    .json({ message: 'username or password not recognized, please try again' });
  return;
}

const validPassword = await userData.checkPassword(req.body.password);

if (!validPassword) {
  res
    .status(400)
    .json({ message: 'Incorrect email or password, please try again' });
  return;
}

req.session.save(() => {
  req.session.user_id = userData.id;
  req.session.logged_in = true;

  res.json({ user: userData, message: 'Thanks for logging in' });
});

} 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;

jujubea22 commented 1 year ago

Completed Monday. Thank you