otherData.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const Merchant = require("../models/merchant");
  2. const bcrypt = require("bcryptjs");
  3. module.exports = {
  4. /*
  5. POST - logs the user in
  6. req.body = {
  7. email: email of the user,
  8. password: password of the user
  9. }
  10. Redirects to /dashboard
  11. */
  12. login: function(req, res){
  13. Merchant.findOne({email: req.body.email.toLowerCase()})
  14. .then((merchant)=>{
  15. if(merchant){
  16. bcrypt.compare(req.body.password, merchant.password, (err, result)=>{
  17. if(result){
  18. req.session.user = merchant.session.sessionId;
  19. return res.redirect("/dashboard");
  20. }else{
  21. req.session.error = "INVALID EMAIL OR PASSWORD";
  22. return res.redirect("/login");
  23. }
  24. });
  25. }else{
  26. req.session.error = "INVALID EMAIL OR PASSWORD";
  27. return res.redirect("/login");
  28. }
  29. })
  30. .catch((err)=>{
  31. req.session.error = "ERROR: UNABLE TO RETRIEVE USER DATA";
  32. return res.redirect("/");
  33. });
  34. },
  35. /*
  36. GET - logs the user out
  37. Redirects to /
  38. */
  39. logout: function(req, res){
  40. req.session.user = null;
  41. return res.redirect("/");
  42. }
  43. }