otherData.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. const bcrypt = require("bcryptjs");
  2. const axios = require("axios");
  3. const Merchant = require("../models/merchant");
  4. const Order = require("../models/order");
  5. const Transaction = require("../models/transaction");
  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. Merchant.findOne({email: req.body.email.toLowerCase()})
  17. .then((merchant)=>{
  18. if(merchant){
  19. bcrypt.compare(req.body.password, merchant.password, (err, result)=>{
  20. if(result){
  21. req.session.user = merchant._id;
  22. return res.redirect("/dashboard");
  23. }else{
  24. req.session.error = "Invalid email or password";
  25. return res.redirect("/");
  26. }
  27. });
  28. }else{
  29. req.session.error = "Invalid email or password";
  30. return res.redirect("/");
  31. }
  32. })
  33. .catch((err)=>{
  34. req.session.error = "There was an error and your data could not be retrieved";
  35. return res.redirect("/");
  36. });
  37. },
  38. /*
  39. GET - logs the user out
  40. Redirects to /
  41. */
  42. logout: function(req, res){
  43. req.session.user = undefined;
  44. return res.redirect("/");
  45. },
  46. //GET - Redirects user to Clover OAuth page
  47. cloverRedirect: function(req, res){
  48. return res.redirect(`${process.env.CLOVER_ADDRESS}/oauth/authorize?client_id=${process.env.SUBLINE_CLOVER_APPID}&redirect_uri=${process.env.SUBLINE_CLOVER_URI}`);
  49. },
  50. //GET - Get access token from clover and redirect to merchant creation
  51. cloverAuth: function(req, res){
  52. let dataArr = req.url.slice(req.url.indexOf("?") + 1).split("&");
  53. let authorizationCode = "";
  54. let merchantId = "";
  55. for(let str of dataArr){
  56. if(str.slice(0, str.indexOf("=")) === "merchant_id"){
  57. merchantId = str.slice(str.indexOf("=") + 1);
  58. }else if(str.slice(0, str.indexOf("=")) === "code"){
  59. authorizationCode = str.slice(str.indexOf("=") + 1);
  60. }
  61. }
  62. axios.get(`${process.env.CLOVER_ADDRESS}/oauth/token?client_id=${process.env.SUBLINE_CLOVER_APPID}&client_secret=${process.env.SUBLINE_CLOVER_APPSECRET}&code=${authorizationCode}`)
  63. .then((response)=>{
  64. Merchant.findOne({posId: merchantId})
  65. .then((merchant)=>{
  66. if(merchant){
  67. merchant.posAccessToken = response.data.access_token;
  68. merchant.save()
  69. .then((updatedMerchant)=>{
  70. req.session.user = updatedMerchant._id;
  71. return res.redirect("/dashboard");
  72. })
  73. .catch((err)=>{
  74. req.session.error("Error: unable to save critical data. Try again.");
  75. return res.redirect("/")
  76. });
  77. }else{
  78. req.session.merchantId = merchantId;
  79. req.session.accessToken = response.data.access_token;
  80. return res.redirect("/merchant/create/clover");
  81. }
  82. })
  83. .catch((err)=>{
  84. req.session.error = "Error: there was an oopsies";
  85. });
  86. })
  87. .catch((err)=>{
  88. req.session.error = "Error: Unable to retrieve data from Clover";
  89. return res.redirect("/");
  90. });
  91. },
  92. /*
  93. POST - Changes the users password
  94. req.body = {
  95. pass: new password,
  96. confirmPass: new password confirmation,
  97. hash: hashed version of old password
  98. }
  99. */
  100. resetPassword: function(req, res){
  101. Merchant.findOne({password: req.body.hash})
  102. .then((merchant)=>{
  103. if(merchant && req.body.pass === req.body.confirmPass){
  104. let salt = bcrypt.genSaltSync(10);
  105. let hash = bcrypt.hashSync(req.body.pass, salt);
  106. merchant.password = hash;
  107. return merchant.save();
  108. }else{
  109. req.session.error = "Error: unable to retrieve merchant data";
  110. return res.redirect("/");
  111. }
  112. })
  113. .then((merchant)=>{
  114. req.session.error = "Password successfully reset. Please log in";
  115. return res.redirect("/");
  116. })
  117. .catch((err)=>{});
  118. }
  119. }