otherData.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. req.session.error = "ERROR: UNABLE TO RETRIEVE DATA";
  62. return res.redirect("/");
  63. });
  64. },
  65. /*
  66. GET - logs the user out
  67. Redirects to /
  68. */
  69. logout: function(req, res){
  70. req.session.owner = undefined;
  71. return res.redirect("/");
  72. },
  73. /*
  74. POST: create user feedback
  75. req.body = {
  76. title: String,
  77. content: String,
  78. date: String (Number, unix time)
  79. }
  80. response = {}
  81. */
  82. feedback: function(req, res){
  83. let feedback = new Feedback({
  84. merchant: res.locals.merchant._id,
  85. title: req.body.title,
  86. content: req.body.content,
  87. date: new Date(req.body.date)
  88. });
  89. feedback.save()
  90. .then((feedback)=>{
  91. return res.json({});
  92. })
  93. .catch((err)=>{
  94. return res.json("ERROR: UNABLE TO SAVE DATA");
  95. });
  96. },
  97. /*
  98. GET: changes the session id and logs user out
  99. redirect = "/"
  100. */
  101. endSession: function(req, res){
  102. let newExpiration = new Date();
  103. newExpiration.setDate(newExpiration.getDate() + 90);
  104. res.locals.owner.session.sessionId = helper.generateId(25);
  105. res.locals.owner.session.expiration = newExpiration;
  106. req.session.owner = undefined;
  107. req.session.merchant = undefined;
  108. res.locals.owner.save()
  109. .then(()=>{
  110. return res.redirect("/");
  111. })
  112. .catch((err)=>{
  113. return res.json("ERROR: SOMETHING WENT WRONG. PLEASE CONTACT SUPPORT.");
  114. });
  115. }
  116. }