Sfoglia il codice sorgente

Finish the verification process for new merchants.
All merchants now MUST verify their email address.

Lee Morgan 5 anni fa
parent
commit
02aaa1c595

+ 68 - 0
controllers/emailVerification.js

@@ -0,0 +1,68 @@
+const Merchant = require("../models/merchant.js");
+
+const mailgun = require("mailgun-js")({apiKey: process.env.MG_SUBLINE_APIKEY, domain: "mail.thesubline.net"});
+const VerifyEmail = require("../emails/verifyEmail.js");
+
+module.exports = {
+    sendVerifyEmail: function(req, res){
+        Merchant.findOne({_id: req.params.id})
+            .then((merchant)=>{
+                const mailgunData = {
+                    from: "The Subline <clientsupport@thesubline.net>",
+                    to: merchant.email,
+                    subject: "Email verification",
+                    html: VerifyEmail({
+                        name: merchant.name,
+                        link: `${process.env.SITE}/verify/${merchant._id}`,
+                        code: merchant.verifyId
+                    })
+                }
+                mailgun.messages().send(mailgunData, (err, body)=>{});
+
+                return res.redirect(`/verify/${merchant._id}`);
+            })
+            .catch((err)=>{
+                req.session.error = "ERROR: UNABLE TO SEND VERIFICATION EMAIL";
+                return res.redirect("/");
+            });
+    },
+
+    verifyPage: function(req, res){
+        return res.render("verifyPage/verify", {id: req.params.id});
+    },
+
+    verify: function(req, res){
+        Merchant.findOne({_id: req.body.id})
+            .then((merchant)=>{
+                if(req.body.code !== merchant.verifyId){
+                    req.session.error = "INCORRECT CODE";
+                    
+                    return res.redirect(`/verify/${merchant._id}`);
+                }
+
+                merchant.verifyId = undefined;
+                merchant.status.splice(merchant.status.indexOf("unverified"), 1);
+
+                const mailgunList = mailgun.lists("clientsupport@mail.thesubline.com");
+                const memberData = {
+                    subscribed: true,
+                    address: merchant.email,
+                    name: merchant.name,
+                    vars: {}
+                }
+                mailgunList.members().create(memberData, (err, data)=>{});
+
+                return merchant.save();
+            })
+            .then((merchant)=>{
+                req.session.user = merchant._id;
+
+                return res.redirect("/dashboard");
+            })
+            .catch((err)=>{
+                req.session.error = "ERROR: UNABLE TO VERIFY EMAIL ADDRESS";
+
+                return res.redirect("/");
+            });
+    }
+}

+ 1 - 1
controllers/helper.js

@@ -243,6 +243,6 @@ module.exports = {
             result += characters.charAt(Math.floor(Math.random() * characters.length));
         }
         
-        return results.length;
+        return result;
     }
 }

+ 1 - 26
controllers/merchantData.js

@@ -1,13 +1,11 @@
 const axios = require("axios");
 const bcrypt = require("bcryptjs");
-const mailgun = require("mailgun-js")({apiKey: process.env.MG_SUBLINE_APIKEY, domain: "mail.thesubline.net"});
 
 const Merchant = require("../models/merchant");
 const Recipe = require("../models/recipe");
 const InventoryAdjustment = require("../models/inventoryAdjustment");
 const Validator = require("./validator.js");
 const Helper = require("./helper.js");
-const WelcomeEmail = require("../emails/welcomeEmail.js");
 
 module.exports = {
     /*
@@ -46,30 +44,7 @@ module.exports = {
 
             merchant.save()
                 .then((merchant)=>{
-                    req.session.user = merchant._id;
-                    const mail = merchant.email.split("@");
-
-                    const mailgunData = {
-                        from: "The Subline <clientsupport@thesubline.net>",
-                        to: merchant.email,
-                        subject: "Welcome to The Subline!",
-                        html: WelcomeEmail({
-                            name: merchant.name,
-                            link: `${process.env.SITE}/verify/${merchant.verifyId}/${mail[0]}`
-                        })
-                    }
-                    mailgun.messages().send(mailgunData, (err, body)=>{});
-
-                    const mailgunList = mailgun.lists("clientsupport@mail.thesubline.com");
-                    const memberData = {
-                        subscribed: true,
-                        address: merchant.email,
-                        name: merchant.name,
-                        vars: {}
-                    }
-                    mailgunList.members().create(memberData, (err, data)=>{});
-
-                    return res.redirect("/dashboard");
+                    return res.redirect(`/verify/email/${merchant._id}`);
                 })
                 .catch((err)=>{
                     req.session.error = "ERROR: UNABLE TO CREATE ACCOUNT AT THIS TIME";

+ 2 - 0
controllers/otherData.js

@@ -1,6 +1,8 @@
 const bcrypt = require("bcryptjs");
 const axios = require("axios");
 const path = require("path");
+const mailgun = require("mailgun-js")({apiKey: process.env.MG_SUBLINE_APIKEY, domain: "mail.thesubline.net"});
+const VerifyEmail = require("../emails/verifyEmail.js");
 
 const Merchant = require("../models/merchant");
 

+ 11 - 6
controllers/renderer.js

@@ -44,6 +44,7 @@ module.exports = {
             req.session.error = "MUST BE LOGGED IN TO DO THAT";
             return res.redirect("/");
         }
+        
         new Activity({
             ipAddr: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
             merchant: req.session.user,
@@ -64,13 +65,18 @@ module.exports = {
                 lastUpdatedTime: 1,
                 inventory: 1,
                 recipes: 1,
-                squareLocation: 1
+                squareLocation: 1,
+                status: 1
             }
         )
             .populate("inventory.ingredient")
             .populate("recipes")
             .then(async (merchant)=>{
                 merchant2 = merchant;
+                if(merchant.status.includes("unverified")){
+                    throw "unverified";
+                }
+
                 if(merchant.pos === "clover"){
                     await helper.getCloverData(merchant);
                 }else if(merchant.pos === "square"){
@@ -105,20 +111,19 @@ module.exports = {
                 merchant2.posAccessToken = undefined;
                 merchant2.lastUpdatedTime = undefined;
                 merchant2.accountStatus = undefined;
+                merchant2.status = undefined;
 
                 return res.render("dashboardPage/dashboard", {merchant: merchant2, transactions: transactions});
             })
             .catch((err)=>{
+                if(err === "unverified"){
+                    return res.redirect(`/verify/email/${merchant2._id}`);
+                }
                 req.session.error = "ERROR: UNABLE TO RETRIEVE USER DATA";
                 return res.redirect("/");
             });
     },
 
-    //GET - Renders the information page
-    displayLegal: function(req, res){
-        return res.render("informationPage/information");
-    },
-
     //GET - Renders the page to reset your password
     displayPassReset: function(req, res){
         return res.render("passResetPage/passReset");

+ 4 - 4
emails/welcomeEmail.js → emails/verifyEmail.js

@@ -1,17 +1,17 @@
 module.exports = (data)=>{
     return `
-        <div id="welcomeEmail">
+        <div id="verifyemail">
             <header style="width:100%;height:75px;background:rgb(0,27,45);">
                 <img src="https://i.postimg.cc/dQky3vPX/logo.png" alt="Subline Logo" style="height:50px;padding:12px 12px;float:left;">
 
                 <h3 style="color:rgb(255,99,107);font-size:30px;margin:15px 0 0 0;float:left;">THE SUBLINE</h3>
             </header>
 
-            <h1 style="text-align:center;">Welcome to The Subline ${data.name}!</h1>
+            <h1 style="text-align:center;">Email Verification for ${data.name}</h1>
 
-            <p>We are glad to have you aboard and look forward to helping your business improve efficiency and cut costs.</p>
+            <p>Please enter the following code on the previous page.  Or use the link below to reopen the page.</p>
 
-            <p>Please follow the link below to verify your email address:</p>
+            <p>CODE: ${data.code}</p>
 
             <p>${data.link}</p>
         </div>

+ 2 - 1
models/merchant.js

@@ -45,7 +45,8 @@ const MerchantSchema = new mongoose.Schema({
     recipes: [{
         type: mongoose.Schema.Types.ObjectId,
         ref: "Recipe"
-    }]
+    }],
+    verifyId: String
 });
 
 module.exports = mongoose.model("Merchant", MerchantSchema);

+ 6 - 0
routes.js

@@ -6,6 +6,7 @@ const transactionData = require("./controllers/transactionData");
 const recipeData = require("./controllers/recipeData");
 const orderData = require("./controllers/orderData.js");
 const informationPages = require("./controllers/informationPages.js");
+const emailVerification = require("./controllers/emailVerification.js");
 
 module.exports = function(app){
     //Render page
@@ -60,4 +61,9 @@ module.exports = function(app){
     app.get("/privacy", informationPages.privacy);
     app.get("/terms", informationPages.terms);
     app.get("/help", informationPages.help);
+
+    //Email verification
+    app.get("/verify/email/:id", emailVerification.sendVerifyEmail);
+    app.get("/verify/:id", emailVerification.verifyPage);
+    app.post("/verify", emailVerification.verify);
 }

+ 25 - 0
views/verifyPage/verify.ejs

@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta charset="utf-8">
+        <meta content="width=device-width, initial-scale=1" name="viewport"/>
+        <title>The Subline</title>
+        <link rel="icon" type="img/png" href="/shared/images/logo.png">
+        <link href="https://fonts.googleapis.com/css?family=Saira&display=swap" rel="stylesheet"> 
+    </head>
+    <body>
+        <h1>Verify your email address</h1>
+
+        <p>Please enter the code sent to your email address</p>
+
+        <form action="/verify" method="post">
+            <input name="code" type="text">
+
+            <input name="id" type="hidden" value="<%=id%>">
+
+            <input type="submit" value="VERIFY">
+        </form>
+
+        <p>Didn't recieve an email? <a href="/verify/email/<%=id%>">Resend email</a></p>
+    </body>
+</html>