From e301605990fd4413e63f360a747af19d448c2962 Mon Sep 17 00:00:00 2001 From: craigfisk Date: Sun, 30 Jun 2024 14:11:26 -0400 Subject: [PATCH 1/8] Git cloned and added "if (location.hostname === 127.0.0.1 etc. to codelab-initial-state/public/js/homepage.js. --- codelab-initial-state/public/js/homepage.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/codelab-initial-state/public/js/homepage.js b/codelab-initial-state/public/js/homepage.js index 7a23e00..957c90e 100644 --- a/codelab-initial-state/public/js/homepage.js +++ b/codelab-initial-state/public/js/homepage.js @@ -28,6 +28,12 @@ export async function onDocumentReady(firebaseApp) { const auth = firebaseApp.auth(); const db = firebaseApp.firestore(); + if (location.hostname === "127.0.0.1") { + console.log("127.0.0.1 detected!"); + auth.useEmulator("http://127.0.0.1:9099"); + db.useEmulator("127.0.0.1", 8080); + } + const homePage = new HomePage(db, auth); mount(document.body, homePage); } From 6eaae8ba6dbbf06aa083cd73cfbc41bf4335fe57 Mon Sep 17 00:00:00 2001 From: craigfisk Date: Sun, 30 Jun 2024 20:41:20 -0400 Subject: [PATCH 2/8] Added "if (this.auth.currentUser === null" in public/js/homepage.js. --- codelab-initial-state/public/js/homepage.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/codelab-initial-state/public/js/homepage.js b/codelab-initial-state/public/js/homepage.js index 957c90e..c36789f 100644 --- a/codelab-initial-state/public/js/homepage.js +++ b/codelab-initial-state/public/js/homepage.js @@ -33,7 +33,7 @@ export async function onDocumentReady(firebaseApp) { auth.useEmulator("http://127.0.0.1:9099"); db.useEmulator("127.0.0.1", 8080); } - + const homePage = new HomePage(db, auth); mount(document.body, homePage); } @@ -176,6 +176,10 @@ class HomePage { } addToCart(id, itemData) { + if (this.auth.currentUser === null) { + throw "Not signed in"; + } + if (this.auth) console.log("addToCart", id, JSON.stringify(itemData)); return this.db .collection("carts") From b3094c5775318b18bc68e3ed919356463ac984a4 Mon Sep 17 00:00:00 2001 From: craigfisk Date: Sun, 30 Jun 2024 21:16:27 -0400 Subject: [PATCH 3/8] Fixed calculation of total $ and # items in codelab-initial-state/functions/index.js. --- codelab-initial-state/functions/index.js | 33 +++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/codelab-initial-state/functions/index.js b/codelab-initial-state/functions/index.js index 4ac0f5c..3d1acc2 100644 --- a/codelab-initial-state/functions/index.js +++ b/codelab-initial-state/functions/index.js @@ -27,16 +27,37 @@ exports.calculateCart = functions return; } - let totalPrice = 125.98; - let itemCount = 8; + let totalPrice = 0; //125.98; + let itemCount = 0; //9; try { - const cartRef = db.collection("carts").doc(context.params.cartId); + const itemsSnap = await cartRef.collection("items").get(); + + itemsSnap.docs.forEach(item => { + const itemData = item.data(); + if (itemData.price) { + // If not specified, the quantity is 1 + const quantity = itemData.quantity ? itemData.quantity : 1; + itemCount += quantity; + totalPrice += (itemData.price * quantity); + } + }); await cartRef.update({ totalPrice, itemCount }); - } catch(err) { - } - }); + + console.log("Updated successfully!"); + } catch(err) { + console.warn("update error", err); + } + }); + + + + + + + + \ No newline at end of file From b40cdaa36ffdc11bf598fb783b66c3d6e18bf2d9 Mon Sep 17 00:00:00 2001 From: craigfisk Date: Sun, 30 Jun 2024 21:23:35 -0400 Subject: [PATCH 4/8] In firestore.rules for section #7, set "false" except for read. --- codelab-initial-state/firestore.rules | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codelab-initial-state/firestore.rules b/codelab-initial-state/firestore.rules index d2c966a..2079094 100644 --- a/codelab-initial-state/firestore.rules +++ b/codelab-initial-state/firestore.rules @@ -4,13 +4,13 @@ service cloud.firestore { // User's cart metadata match /carts/{cartID} { // TODO: Change these! Anyone can read or write. - allow read, write: if true; + allow read, write: if false; } // Items inside the user's cart match /carts/{cartID}/items/{itemID} { // TODO: Change these! Anyone can read or write. - allow read, write: if true; + allow read, write: if false; } // All items available in the store. Users can read From 07e3aae957607e284011091a8be95efddc0a9e2b Mon Sep 17 00:00:00 2001 From: craigfisk Date: Sun, 30 Jun 2024 21:33:33 -0400 Subject: [PATCH 5/8] In #9, changed firestore.rules to allow read,write when auth.uid == ownerUID, so now 2 pass, 2 fail, 1 pending. --- codelab-initial-state/firestore.rules | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/codelab-initial-state/firestore.rules b/codelab-initial-state/firestore.rules index 2079094..96cb12c 100644 --- a/codelab-initial-state/firestore.rules +++ b/codelab-initial-state/firestore.rules @@ -4,7 +4,8 @@ service cloud.firestore { // User's cart metadata match /carts/{cartID} { // TODO: Change these! Anyone can read or write. - allow read, write: if false; + allow create: if request.auth.uid == request.resource.data.ownerUID; + allow read, update, delete: if request.auth.uid == resource.data.ownerUID; } // Items inside the user's cart From 78cb1dc709c2fdd7ded9713da355fcb24781d363 Mon Sep 17 00:00:00 2001 From: craigfisk Date: Sun, 30 Jun 2024 21:40:13 -0400 Subject: [PATCH 6/8] Change 2nd firestore rule to allow read, write for subcollections, so now 4 pass, 1 fail. --- codelab-initial-state/firestore.rules | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/codelab-initial-state/firestore.rules b/codelab-initial-state/firestore.rules index 96cb12c..3b7a038 100644 --- a/codelab-initial-state/firestore.rules +++ b/codelab-initial-state/firestore.rules @@ -3,7 +3,7 @@ service cloud.firestore { match /databases/{database}/documents { // User's cart metadata match /carts/{cartID} { - // TODO: Change these! Anyone can read or write. + // TODO: Changed so read or write only ownerUID == logged in auth UID. allow create: if request.auth.uid == request.resource.data.ownerUID; allow read, update, delete: if request.auth.uid == resource.data.ownerUID; } @@ -11,7 +11,8 @@ service cloud.firestore { // Items inside the user's cart match /carts/{cartID}/items/{itemID} { // TODO: Change these! Anyone can read or write. - allow read, write: if false; + // allow read, write: if false; + allow read, write: if get(/databases/$(database)/documents/carts/$(cartID)).data.ownerUID == request.auth.uid; } // All items available in the store. Users can read From c35ffa07eed8150f05806715f80bb1183ea6e269 Mon Sep 17 00:00:00 2001 From: craigfisk Date: Wed, 3 Jul 2024 16:24:20 -0400 Subject: [PATCH 7/8] In "codelab-initial-state/functions/test", changed "changeme" to "marmalade-beta" as REAL_FIREBASE_PROJECT-ID in "test.js", and changed #182 "describe.skip" to "describe". --- codelab-initial-state/functions/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codelab-initial-state/functions/test.js b/codelab-initial-state/functions/test.js index e767591..2b4f7af 100755 --- a/codelab-initial-state/functions/test.js +++ b/codelab-initial-state/functions/test.js @@ -17,7 +17,7 @@ const path = require("path"); const TEST_FIREBASE_PROJECT_ID = "test-firestore-rules-project"; // TODO: Change this to your real Firebase Project ID -const REAL_FIREBASE_PROJECT_ID = "changeme"; +const REAL_FIREBASE_PROJECT_ID = "marmalade-beta"; //"changeme"; const firebase = require("@firebase/rules-unit-testing"); @@ -179,7 +179,7 @@ describe("shopping cart items", async () => { }); }); -describe.skip("adding an item to the cart recalculates the cart total. ", () => { +describe("adding an item to the cart recalculates the cart total. ", () => { const admin = firebase.initializeAdminApp({ projectId: REAL_FIREBASE_PROJECT_ID }).firestore(); From a6c10d4200ad2843d0cb8a5b333ca0ec41131c9c Mon Sep 17 00:00:00 2001 From: craigfisk Date: Wed, 3 Jul 2024 16:36:06 -0400 Subject: [PATCH 8/8] In #18, changed initial values to 0 in functions/index.js "calculateCart" so that all 5 tests now pass, and expanded console.log message in #51 to say "Cart total successfully recalculated!". --- codelab-initial-state/functions/index.js | 2 +- codelab-initial-state/functions/test.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/codelab-initial-state/functions/index.js b/codelab-initial-state/functions/index.js index 3d1acc2..b6b0bbe 100644 --- a/codelab-initial-state/functions/index.js +++ b/codelab-initial-state/functions/index.js @@ -48,7 +48,7 @@ exports.calculateCart = functions itemCount }); - console.log("Updated successfully!"); + console.log("Cart total successfully recalculated!"); } catch(err) { console.warn("update error", err); } diff --git a/codelab-initial-state/functions/test.js b/codelab-initial-state/functions/test.js index 2b4f7af..57e492e 100755 --- a/codelab-initial-state/functions/test.js +++ b/codelab-initial-state/functions/test.js @@ -17,6 +17,7 @@ const path = require("path"); const TEST_FIREBASE_PROJECT_ID = "test-firestore-rules-project"; // TODO: Change this to your real Firebase Project ID +// CF: is this the correct PROJECT_ID? const REAL_FIREBASE_PROJECT_ID = "marmalade-beta"; //"changeme"; const firebase = require("@firebase/rules-unit-testing");