-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
95 lines (75 loc) · 2.13 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const express = require("express");
const { firestore } = require("firebase-admin");
const app = express();
const port = 8080;
const admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.applicationDefault()
// credential: admin.credential.cert(key)
});
const db = admin.firestore();
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
app.use(express.json())
app.post("/createUser", (req, res) => {
// the authentication stuff:
const {uid} = req.body;
const additionalClaims = {
premiumAccount: true
}
admin.auth().createCustomToken(uid, additionalClaims)
.then((customToken) => {
res.json({jwt: customToken})
})
.catch((error) => {
console.log("error creating token:", error)
})
})
app.put("/colour", (req, res) => {
const getTile = async () => {
return {
hex: req.body.hex,
location: new admin.firestore.Firestore.GeoPoint(req.body.location.latitude, req.body.location.longitude),
uid: req.body.user
};
};
getTile().then((result) => {
return db.collection("tiles").add(result);
})
.catch((error) => console.error(error))
.then(() => {
res.send("new tile added!");
});
});
app.post("/colour", (req, res) => {
const getTile = async () => {
return {
hex: req.body.hex,
location: new admin.firestore.Firestore.GeoPoint(req.body.location.latitude, req.body.location.longitude),
uid: req.body.user
};
};
getTile().then((result) => {
return db.collection("tiles").add(result);
})
.catch((error) => console.error(error))
.then(() => {
res.send("new tile added!");
});
})
app.get("/tiles", async (req, res) => {
const { userLocation, locationThreshold } = req.body;
const tiles = [];
const snapshot = await db.collection('tiles').get();
snapshot.forEach((doc) => {
tiles.push({
id: doc.id,
...doc.data()
})
});
// Keren will filter the tiles based on _latitude and _longitude
res.json({
tiles
})
})