Selaa lähdekoodia

Update displaying of the dashboard to use new session id system.

Lee Morgan 5 vuotta sitten
vanhempi
sitoutus
c74a63983e
4 muutettua tiedostoa jossa 45 lisäystä ja 37 poistoa
  1. 6 3
      app.js
  2. 8 1
      controllers/merchantData.js
  3. 1 1
      controllers/otherData.js
  4. 30 32
      controllers/renderer.js

+ 6 - 3
app.js

@@ -7,7 +7,9 @@ const fs = require("fs");
 const Merchant = require("./models/merchant.js");
 const helper = require("./controllers/helper.js");
 
-let protectedRoutes = [];
+let protectedRoutes = [
+    "/dashboard"
+];
 
 const app = express();
 
@@ -42,7 +44,6 @@ app.use(session({
 app.use((req, res, next)=>{
     if(protectedRoutes.includes(req.url)){
         if(req.session.user === undefined) return res.redirect("/");
-
         Merchant.findOne({"session.sessionId": req.session.user})
             .then((merchant)=>{
                 if(merchant === null){
@@ -64,10 +65,12 @@ app.use((req, res, next)=>{
             })
             .catch((err)=>{
                 if(err === "no merchant"){
-                    return res.redirect("PLEASE LOG IN");
+                    return res.redirect("/");
                 }
                 return res.json("ERROR: UNABLE TO RETRIEVE DATA");
             });
+    }else{
+        return next();
     }
 });
 app.use(express.urlencoded({extended: true}));

+ 8 - 1
controllers/merchantData.js

@@ -38,6 +38,9 @@ module.exports = {
         let salt = bcrypt.genSaltSync(10);
         let hash = bcrypt.hashSync(req.body.password, salt);
 
+        let expirationDate = new Date();
+        expirationDate.setDate(expirationDate.getDate() + 90);
+
         let merchant = new Merchant({
             name: req.body.name,
             email: req.body.email.toLowerCase(),
@@ -48,7 +51,11 @@ module.exports = {
             status: ["unverified"],
             inventory: [],
             recipes: [],
-            verifyId: helper.generateId(15)
+            verifyId: helper.generateId(15),
+            session: {
+                sessionId: helper.generateId(25),
+                expiration: expirationDate
+            }
         });
 
         merchant.save()

+ 1 - 1
controllers/otherData.js

@@ -19,7 +19,7 @@ module.exports = {
                 if(merchant){
                     bcrypt.compare(req.body.password, merchant.password, (err, result)=>{
                         if(result){
-                            req.session.user = merchant._id;
+                            req.session.user = merchant.session.sessionId;
                             return res.redirect("/dashboard");
                         }else{
                             req.session.error = "INVALID EMAIL OR PASSWORD";

+ 30 - 32
controllers/renderer.js

@@ -13,7 +13,6 @@ module.exports = {
     Renders landingPage
     */
     landingPage: function(req, res){
-        console.log(res.locals.otherData);
         new Activity({
             ipAddr: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
             merchant: req.session.user,
@@ -61,49 +60,48 @@ module.exports = {
             .save()
             .catch(()=>{});
 
+
         let merchant2 = {};
-        Merchant.findOne(
-            {_id: req.session.user},
-            {
-                name: 1,
-                pos: 1,
-                posId: 1,
-                posAccessToken: 1,
-                lastUpdatedTime: 1,
-                inventory: 1,
-                recipes: 1,
-                squareLocation: 1,
-                status: 1
-            }
-        )
+        // Merchant.findOne(
+        //     {_id: res.locals.merchant._id},
+        //     {
+        //         name: 1,
+        //         pos: 1,
+        //         posId: 1,
+        //         posAccessToken: 1,
+        //         lastUpdatedTime: 1,
+        //         inventory: 1,
+        //         recipes: 1,
+        //         squareLocation: 1,
+        //         status: 1
+        //     }
+        // )
+        res.locals.merchant
             .populate("inventory.ingredient")
             .populate("recipes")
+            .execPopulate()
             .then(async (merchant)=>{
-                merchant2 = merchant;
-                if(merchant.status.includes("unverified")){
+                if(res.locals.merchant.status.includes("unverified")){
                     throw "unverified";
                 }
 
-                if(merchant.pos === "clover"){
-                    await helper.getCloverData(merchant);
-                }else if(merchant.pos === "square"){
-                    await helper.getSquareData(merchant);
+                if(res.locals.merchant.pos === "clover"){
+                    await helper.getCloverData(res.locals.merchant);
+                }else if(res.locals.merchant.pos === "square"){
+                    await helper.getSquareData(res.locals.merchant);
                 }else{
                     return;
                 }
 
-                return merchant.save();
+                return res.locals.merchant.save();
             })
             .then((merchant)=>{
-                if(merchant){
-                    merchant2 = merchant;
-                }
                 let date = new Date();
                 let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
 
                 return Transaction.aggregate([
                     {$match: {
-                        merchant: new ObjectId(req.session.user),
+                        merchant: new ObjectId(res.locals.merchant._id),
                         date: {$gte: firstDay},
                     }},
                     {$sort: {date: -1}},
@@ -114,13 +112,13 @@ module.exports = {
                 ]);      
             })
             .then((transactions)=>{
-                merchant2._id = undefined;
-                merchant2.posAccessToken = undefined;
-                merchant2.lastUpdatedTime = undefined;
-                merchant2.accountStatus = undefined;
-                merchant2.status = undefined;
+                res.locals.merchant._id = undefined;
+                res.locals.merchant.posAccessToken = undefined;
+                res.locals.merchant.lastUpdatedTime = undefined;
+                res.locals.merchant.accountStatus = undefined;
+                res.locals.merchant.status = undefined;
 
-                return res.render("dashboardPage/dashboard", {merchant: merchant2, transactions: transactions});
+                return res.render("dashboardPage/dashboard", {merchant: res.locals.merchant, transactions: transactions});
             })
             .catch((err)=>{
                 if(err === "unverified"){