Update
This commit is contained in:
parent
39b6b2041c
commit
8da8d3cdf4
@ -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)
|
exports.application = functions.https.onRequest(app)
|
||||||
@ -533,3 +533,11 @@ footer a {
|
|||||||
.bottom {
|
.bottom {
|
||||||
padding-top: 50px;
|
padding-top: 50px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.purchase-message {
|
||||||
|
color: #00df90;
|
||||||
|
text-align: center;
|
||||||
|
font-size: 25px;
|
||||||
|
font-family: osl;
|
||||||
|
padding-bottom: 10px;
|
||||||
|
}
|
||||||
@ -98,9 +98,45 @@ updateImgs();
|
|||||||
function purchaseItem() {
|
function purchaseItem() {
|
||||||
const purchaseBtn = document.getElementById('remember-to-add-auth-verificartion-to-js-handler')
|
const purchaseBtn = document.getElementById('remember-to-add-auth-verificartion-to-js-handler')
|
||||||
if (purchaseBtn.innerHTML == "Buy!") {
|
if (purchaseBtn.innerHTML == "Buy!") {
|
||||||
purchaseBtn.innerHTML = "Functionality not yet implemented 😢"
|
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;");
|
purchaseBtn.setAttribute("style", "width: 500px;");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
purchaseBtn.disabled = true
|
purchaseBtn.disabled = true
|
||||||
}
|
}
|
||||||
|
|||||||
@ -58,8 +58,9 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="space"></div>
|
<div class="space"></div>
|
||||||
|
<div id="api-response" class="purchase-message" style="display: none;">Congratulations, you own the item</div>
|
||||||
|
|
||||||
|
<div id="productID" style="display: none;" product_id="<%= key%>"></div>
|
||||||
<div class="product-page-container">
|
<div class="product-page-container">
|
||||||
<div class="product-page-info-container">
|
<div class="product-page-info-container">
|
||||||
<div class="product-page-img"><img class="dyn-img" src="media/noimage.png" title="<%= item.img %>"></div>
|
<div class="product-page-img"><img class="dyn-img" src="media/noimage.png" title="<%= item.img %>"></div>
|
||||||
|
|||||||
23
server.js
23
server.js
@ -99,7 +99,7 @@ app.get('/product', function(request, responce){
|
|||||||
dbRef.once('value', function(snapshot){
|
dbRef.once('value', function(snapshot){
|
||||||
data = snapshot.val()
|
data = snapshot.val()
|
||||||
if (data){
|
if (data){
|
||||||
responce.render('product.ejs', {item:data})
|
responce.render('product.ejs', {item:data, key:request.query.productid})
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
responce.render('404.ejs')
|
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
|
var port = 5000
|
||||||
|
|
||||||
app.listen(port, function() {
|
app.listen(port, function() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user