otherData.js 4.9 KB

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