otherData.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const Owner = require("../models/owner.js");
  2. const Merchant = require("../models/merchant.js");
  3. const Feedback = require("../models/feedback.js");
  4. const helper = require("./helper.js");
  5. const bcrypt = require("bcryptjs");
  6. module.exports = {
  7. /*
  8. POST - logs the user in
  9. req.body = {
  10. email: email of the user,
  11. password: password of the user
  12. }
  13. Redirects to /dashboard
  14. */
  15. login: function(req, res){
  16. let owner = Owner.findOne({email: req.body.email.toLowerCase()});
  17. let merchant = Merchant.findOne({_id: req.session.merchant});
  18. Promise.all([owner, merchant])
  19. .then((response)=>{
  20. if(response[0] !== null){
  21. bcrypt.compare(req.body.password, response[0].password, async (err, result)=>{
  22. if(result === true){
  23. //Check if email has not been verified
  24. if(response[0].status.includes("unverified")){
  25. req.session.error = "PLEASE VERIFY YOUR EMAIL ADDRESS";
  26. return res.redirect(`/verify/email/${response[0]._id}`);
  27. }
  28. //Check for suspended account
  29. if(response[0].status.includes("suspended")){
  30. req.session.error = "ACCOUNT SUSPENDED. PLEASE CONTACT SUPPORT IF THIS IS IN ERROR";
  31. return res.redirect("/");
  32. }
  33. //Check for out of date access token
  34. let cutoff = new Date();
  35. cutoff.setDate(cutoff.getDate() + 1);
  36. if(response[0].square !== undefined && response[0].square.expires < cutoff){
  37. let data = await axios.post(`${process.env.SQUARE_ADDRESS}/oauth2/token`, {
  38. client_id: process.env.SUBLINE_SQUARE_APPID,
  39. client_secret: process.env.SUBLINE_SQUARE_APPSECRET,
  40. grant_type: "refresh_token",
  41. refresh_token: merchant.square.refreshToken
  42. });
  43. response[0].square.accessToken = data.data.access_token;
  44. response[0].square.expires = new Date(data.data.expires_at);
  45. await response[0].save();
  46. }
  47. req.session.merchant = (response[1] === null) ? await Merchant.findOne({_id: response[0].merchants[0]}) : response[1];
  48. req.session.owner = response[0].session.sessionId;
  49. return res.redirect("/dashboard");
  50. }else{
  51. req.session.error = "INVALID EMAIL OR PASSWORD";
  52. return res.redirect("/login");
  53. }
  54. });
  55. }else{
  56. req.session.error = "INVALID EMAIL OR PASSWORD";
  57. return res.redirect("/login");
  58. }
  59. })
  60. .catch((err)=>{
  61. console.log(err);
  62. req.session.error = "ERROR: UNABLE TO RETRIEVE DATA";
  63. return res.redirect("/");
  64. });
  65. },
  66. /*
  67. GET - logs the user out
  68. Redirects to /
  69. */
  70. logout: function(req, res){
  71. req.session.owner = undefined;
  72. return res.redirect("/");
  73. },
  74. /*
  75. POST: create user feedback
  76. req.body = {
  77. title: String,
  78. content: String,
  79. date: String (Number, unix time)
  80. }
  81. response = {}
  82. */
  83. feedback: function(req, res){
  84. let feedback = new Feedback({
  85. merchant: res.locals.merchant._id,
  86. title: req.body.title,
  87. content: req.body.content,
  88. date: new Date(req.body.date)
  89. });
  90. feedback.save()
  91. .then((feedback)=>{
  92. return res.json({});
  93. })
  94. .catch((err)=>{
  95. return res.json("ERROR: UNABLE TO SAVE DATA");
  96. });
  97. },
  98. /*
  99. GET: changes the session id and logs user out
  100. redirect = "/"
  101. */
  102. endSession: function(req, res){
  103. let newExpiration = new Date();
  104. newExpiration.setDate(newExpiration.getDate() + 90);
  105. res.locals.owner.session.sessionId = helper.generateId(25);
  106. res.locals.owner.session.expiration = newExpiration;
  107. req.session.owner = undefined;
  108. req.session.merchant = undefined;
  109. res.locals.owner.save()
  110. .then(()=>{
  111. return res.redirect("/");
  112. })
  113. .catch((err)=>{
  114. return res.json("ERROR: SOMETHING WENT WRONG. PLEASE CONTACT SUPPORT.");
  115. });
  116. }
  117. }