From 8da8d3cdf44968a05cc7367857fce6c3ed796c4a Mon Sep 17 00:00:00 2001 From: Max Hunt Date: Sun, 26 Apr 2020 18:41:59 +0100 Subject: [PATCH] Update --- functions/index.js | 19 +++++++++++++++++ functions/views/css/style.css | 8 +++++++ functions/views/js/fdb.js | 40 +++++++++++++++++++++++++++++++++-- functions/views/product.ejs | 3 ++- server.js | 23 +++++++++++++++++++- 5 files changed, 89 insertions(+), 4 deletions(-) diff --git a/functions/index.js b/functions/index.js index 2329022..9ab6690 100644 --- a/functions/index.js +++ b/functions/index.js @@ -114,4 +114,23 @@ app.get('/product', function(request, responce){ } }) +app.post('/purchase-api', function(request, responce){ + if (request.body.item && request.body.user) { + admin.auth().verifyIdToken(request.body.user) + .then(function(decodedToken) { + let uid = decodedToken.uid; + let product = request.body.item + var productRef = database.ref("/products/"+product) + productRef.update({owner:uid}) + responce.send({success:true, error:null}) + }) + .catch(function(error) { + responce.send({ error: 'invalid auth token' }) + }); + } + else { + responce.send({ error: 'invalid data' }) + } +}) + exports.application = functions.https.onRequest(app) \ No newline at end of file diff --git a/functions/views/css/style.css b/functions/views/css/style.css index 77e2aee..cc56fa2 100644 --- a/functions/views/css/style.css +++ b/functions/views/css/style.css @@ -532,4 +532,12 @@ footer a { } .bottom { padding-top: 50px; +} + +.purchase-message { + color: #00df90; + text-align: center; + font-size: 25px; + font-family: osl; + padding-bottom: 10px; } \ No newline at end of file diff --git a/functions/views/js/fdb.js b/functions/views/js/fdb.js index de5fa92..31c1b52 100644 --- a/functions/views/js/fdb.js +++ b/functions/views/js/fdb.js @@ -98,8 +98,44 @@ updateImgs(); function purchaseItem() { const purchaseBtn = document.getElementById('remember-to-add-auth-verificartion-to-js-handler') if (purchaseBtn.innerHTML == "Buy!") { - purchaseBtn.innerHTML = "Functionality not yet implemented 😢" - purchaseBtn.setAttribute("style", "width: 500px;"); + if (firebase.auth().currentUser) { + firebase.auth().currentUser.getIdToken().then(function(idToken) { + var item_id_element = document.getElementById("productID") + var item_id = item_id_element.getAttribute("product_id") + const url = 'purchase-api'; + // The data we are going to send in our request + let data = { + item: item_id, + user: idToken + } + // The parameters we are gonna pass to the fetch function + let postData = { + method: 'POST', + body: JSON.stringify(data), + headers: {'Content-Type': 'application/json'} + } + fetch(url, postData) + .then(response => response.json()) + .then(function(result){ + console.log(result) + if (result.success == true) { + document.getElementById("api-response").setAttribute("style", "display: block") + } + else { + document.getElementById("api-response").innerHTML = "Error: " + result.error + document.getElementById("api-response").setAttribute("style", "display: block") + document.getElementById("api-response").setAttribute("style", "color: #DD0000") + } + //check return value + //update element accordingly + }) + }) + } + else { + purchaseBtn.innerHTML = "Please log in to continue" + purchaseBtn.setAttribute("style", "width: 500px;"); + } + } else { purchaseBtn.disabled = true diff --git a/functions/views/product.ejs b/functions/views/product.ejs index 96a2756..d96be65 100644 --- a/functions/views/product.ejs +++ b/functions/views/product.ejs @@ -58,8 +58,9 @@
+ - +
diff --git a/server.js b/server.js index 7ad5cbb..6eb3809 100644 --- a/server.js +++ b/server.js @@ -99,7 +99,7 @@ app.get('/product', function(request, responce){ dbRef.once('value', function(snapshot){ data = snapshot.val() if (data){ - responce.render('product.ejs', {item:data}) + responce.render('product.ejs', {item:data, key:request.query.productid}) } else { responce.render('404.ejs') @@ -111,6 +111,27 @@ app.get('/product', function(request, responce){ } }) +app.post('/purchase-api', function(request, responce){ + console.log(request.body) + if (request.body.item && request.body.user) { + admin.auth().verifyIdToken(request.body.user) + .then(function(decodedToken) { + let uid = decodedToken.uid; + let product = request.body.item + var productRef = database.ref("/products/"+product) + productRef.update({owner:uid}) + responce.send({success:true, error:null}) + }) + .catch(function(error) { + console.log(error) + responce.send({ error: 'invalid auth token' }) + }); + } + else { + responce.send({ error: 'invalid data' }) + } +}) + var port = 5000 app.listen(port, function() {