emailVerification.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const Merchant = require("../models/merchant.js");
  2. const mailgun = require("mailgun-js")({apiKey: process.env.MG_SUBLINE_APIKEY, domain: "mail.thesubline.net"});
  3. const verifyEmail = require("../emails/verifyEmail.js");
  4. const { db } = require("../models/merchant.js");
  5. module.exports = {
  6. sendVerifyEmail: function(req, res){
  7. Merchant.findOne({_id: req.params.id})
  8. .then((merchant)=>{
  9. const mailgunData = {
  10. from: "The Subline <clientsupport@thesubline.net>",
  11. to: merchant.email,
  12. subject: "Email verification",
  13. html: verifyEmail({
  14. name: merchant.name,
  15. link: `${process.env.SITE}/verify/${merchant._id}/${merchant.verifyId}`,
  16. })
  17. };
  18. mailgun.messages().send(mailgunData, (err, body)=>{});
  19. return res.render(`verifyPage/verify`, {id: merchant._id, email: merchant.email});
  20. })
  21. .catch((err)=>{
  22. req.session.error = "ERROR: UNABLE TO SEND VERIFICATION EMAIL";
  23. return res.redirect("/");
  24. });
  25. },
  26. resendEmail: function(req, res){
  27. Merchant.findOne({email: req.body.email.toLowerCase()})
  28. .then((merchant)=>{
  29. if(merchant){
  30. throw "USER WITH THIS EMAIL ADDRESS ALREADY EXISTS";
  31. }
  32. return Merchant.findOne({_id: req.body.id});
  33. })
  34. .then((merchant)=>{
  35. merchant.email = req.body.email.toLowerCase();
  36. return merchant.save();
  37. })
  38. .then((merchant)=>{
  39. return res.redirect(`/verify/email/${merchant._id}`);
  40. })
  41. .catch((err)=>{
  42. if(typeof(err) === "string"){
  43. req.session.error = err;
  44. }else if(err.name === "ValidationError"){
  45. req.session.error = err.errors[Object.keys(err.errors)[0]].properties.message;
  46. }else{
  47. req.session.error = "ERROR: UNABLE TO CHANGE YOUR EMAIL ADDRESS";
  48. }
  49. return res.redirect("/");
  50. });
  51. },
  52. verify: function(req, res){
  53. Merchant.findOne({_id: req.params.id})
  54. .then((merchant)=>{
  55. if(req.params.code !== merchant.verifyId){
  56. throw "UNABLE TO VERIFY EMAIL ADDRESS. INCORRECT LINK";
  57. }
  58. merchant.verifyId = undefined;
  59. merchant.status.splice(merchant.status.indexOf("unverified"), 1);
  60. const mailgunList = mailgun.lists("clientsupport@mail.thesubline.com");
  61. const memberData = {
  62. subscribed: true,
  63. address: merchant.email,
  64. name: merchant.name,
  65. vars: {}
  66. }
  67. mailgunList.members().create(memberData, (err, data)=>{});
  68. return merchant.save();
  69. })
  70. .then((merchant)=>{
  71. req.session.user = merchant._id;
  72. return res.redirect("/dashboard");
  73. })
  74. .catch((err)=>{
  75. if(typeof(err) === "string"){
  76. req.session.error = err;
  77. }else{
  78. req.session.error = "ERROR: UNABLE TO VERIFY EMAIL ADDRESS"
  79. }
  80. return res.redirect("/");
  81. });
  82. }
  83. }