Преглед изворни кода

Add redirects for clover approval

Lee Morgan пре 6 година
родитељ
комит
3f6281a788

+ 22 - 2
controllers/otherData.js

@@ -154,12 +154,32 @@ module.exports = {
             });
     },
 
+    clover: async function(req, res){
+        if(req.url.includes("?")){
+            let urlArgs = req.url.slice(req.url.indexOf("?") + 1).split("&");
+            for(let str of urlArgs){
+                if(str.slice(0, str.indexOf("=")) === "merchant_id"){
+                    let mId = str.slice(str.indexOf("=") + 1);
+                    let merchant = await Merchant.findOne({posId: mId});
+                    if(merchant){
+                        req.session.isLoggedIn = true;
+                        return res.redirect("/");
+                    }else{
+                        return res.redirect("/cloverlogin");
+                    }
+                }
+            }
+        }
+
+        return res.redirect("/");
+    },
+
     //GET - Redirects user to Clover OAuth page
-    clover: function(req, res){
+    cloverRedirect: function(req, res){
         return res.redirect(`${process.env.CLOVER_ADDRESS}/oauth/authorize?client_id=${process.env.SUBLINE_CLOVER_APPID}&redirect_uri=${process.env.SUBLINE_CLOVER_URI}`);
     },
 
-    //GET - Get access token from clover and  redirect to mearchant creation
+    //GET - Get access token from clover and  redirect to merchant creation
     cloverAuth: function(req, res){
         let dataArr = req.url.slice(req.url.indexOf("?") + 1).split("&");
         let authorizationCode = "";

+ 2 - 1
controllers/renderer.js

@@ -13,6 +13,7 @@ module.exports = {
     //Renders landingPage
     landingPage: function(req, res){
         let error = {};
+        let isLoggedIn = req.session.isLoggedIn || false;
         if(req.session.error){
             error = req.session.error;
             req.session.error = undefined;
@@ -20,7 +21,7 @@ module.exports = {
             error = null;
         }
 
-        return res.render("landingPage/landing", {error: error});
+        return res.render("landingPage/landing", {error: error, isLoggedIn: isLoggedIn});
     },
 
     //GET - Displays the main inventory page for merchants

+ 2 - 1
routes.js

@@ -38,7 +38,8 @@ module.exports = function(app){
     app.post("/login", otherData.login);
     app.get("/logout", otherData.logout);
     app.post("/email", otherData.checkUniqueEmail);
-    app.get("/cloverlogin", otherData.clover);
+    app.get("/clover", otherData.clover);
+    app.get("/cloverlogin", otherData.cloverRedirect);
     app.get("/cloverauth*", otherData.cloverAuth);
 
     //Transactions

+ 4 - 1
views/landingPage/landing.ejs

@@ -86,7 +86,10 @@
 
         <% include ../shared/footer %>
 
-        <script>let error = <%- JSON.stringify(error) %>;</script>
+        <script>
+            let error = <%- JSON.stringify(error) %>;
+            let isLoggedIn = <%- JSON.stringify(isLoggedIn) %>;
+        </script>
         <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
         <script src="../shared/validation.js"></script>
         <script src="/landingPage/public.js"></script>

+ 1 - 1
views/landingPage/login.js

@@ -1,5 +1,5 @@
 let loginObj = {
-    display: function(){
+    display: function(username){
         controller.clearScreen();
         controller.loginStrand.style.display = "flex";
     },

+ 4 - 0
views/landingPage/public.js

@@ -2,5 +2,9 @@ let publicObj = {
     display: function(){
         controller.clearScreen();
         controller.publicStrand.style.display = "flex";
+
+        if(isLoggedIn){
+            loginObj.display();
+        }
     }
 }