-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
186 lines (155 loc) · 5.21 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
var express = require('express');
var firebase = require('firebase');
var bodyParser = require('body-parser');
//firebase auth ------------------------------
//insert your info here
firebase.initializeApp({
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
});
const auth = firebase.auth();
//creates a new user
const doCreateUserWithEmailAndPassword = (email, password) =>
auth.createUserWithEmailAndPassword(email, password);
//signs in
const doSignInWithEmailAndPassword = (email, password) =>
auth.signInWithEmailAndPassword(email, password);
//signs out
const doSignOut = () =>
auth.signOut();
//if the user logs in or out this function is called and sets the currentUser variable
firebase.auth().onAuthStateChanged(function(user) {
currentUser = user;
});
//current logged in user is saved in this var
var currentUser = null;
//firebase db ---------------------------------
const db = firebase.database();
//adds a measurement
const doAddWeightMeasurement = (id, timestamp, weight, latitude, longitude) =>
db.ref(`weight/${id}`).push({
timestamp,
weight,
latitude,
longitude
});
//gets all the weight data for a specific user
const onceGetWeight = (id) =>
db.ref('weight/'+ id).once('value');
//middleware ------------------------------
//used for express
var app = express();
//used to serve the css and js files for the client
app.use(express.static('public'));
//I use pug(aka Jade) for generating html
app.set('view engine','pug');
//used to parse the body for the client
app.use(bodyParser.urlencoded({extended: true}));
//I use this in layout.pug to check if the user is logged in, if he/she is it will display the different links in the navbar
app.use(function(req, res, next) {
res.locals.loggedIn = currentUser;
next();
});
//routes ---------------------------------
//if you are logged in, render index else render login page
app.get('/',function (req,res) {
if(currentUser){
res.render('index');
}
else{
res.render('login');
}
});
//just to get rid of the favicon error
app.get('/favicon.ico', (req, res) => res.status(204));
//render signup
app.get('/signup',function(req,res){
res.render('signup');
})
//post for signup, uses firebase db function to create a new user with email and password
//if there is an error I simply send the user to a blank page with the error message
app.post('/signup',function(req,res){
let email = req.body.email;
let password = req.body.password;
doCreateUserWithEmailAndPassword(email, password)
.then(result => {
res.redirect('/');
})
.catch(error => {
res.send(error.message);
});
})
//renders the login page
app.get('/login',function(req,res){
res.render('login');
})
//post for login, tries to sign in using firebase, if succesful redirect to / else redirect to login again
//if there is an error I simply send the user to a blank page with the error message
app.post('/login',function(req,res){
let email = req.body.email;
let password = req.body.password;
doSignInWithEmailAndPassword(email, password)
.then(() => {
res.redirect('/');
})
.catch(error => {
res.send(error.message);
});
})
//logout function, redirects to /
app.get('/logout',function(req,res){
doSignOut();
res.redirect('/');
})
//if the user is logged in, render addMeasure else redirect to /
app.get('/addMeasure',function(req,res){
if(currentUser){
res.render('addMeasure');
}
else{
res.redirect('/');
}
})
//adds a weight measurement, if any of the values are NaN or 0 it throws an error, else it adds the measurement
app.post('/addMeasure',function(req,res){
let timestamp = Number(req.body.timestamp);
let weight = Number(req.body.weight);
let latitude = Number(req.body.latitude);
let longitude = Number(req.body.longitude);
//validate that all inputs are valid (numbers and greater than 0)
if(timestamp == NaN || weight == NaN || latitude == NaN || longitude == NaN){
throw "All inputs needs to be numbers, remember to use dot instead of comma for decimals"
}
else if(timestamp == 0 || weight == 0 || latitude == 0 || longitude == 0){
throw "All numbers need to be greater than 0"
}
//add weight and geolocation to db for the logged in user
doAddWeightMeasurement(currentUser.uid, timestamp, weight,latitude,longitude)
.catch(error => {
res.send(error.message);
});
res.redirect('/');
})
/*app.get('/getWeight/:timestampPast/:timestampFuture', function(req, res) {
let timestampPast = req.params.timestampPast;
let timestampFuture = req.params.timestampFuture;
console.log(timestampPast)
console.log(timestampFuture)
onceGetWeight(currentUser.uid)
.then(function(result){
res.json(result);
});
})*/
//gets all the weight data for the user
//I wanted to send an range query for the timestamp but couldn't figure out how it worked with firebase.
app.get('/getWeight/', function(req, res) {
onceGetWeight(currentUser.uid)
.then(function(result){
res.json(result);
});
})
var server = app.listen(1337);