-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
349 lines (310 loc) · 10.4 KB
/
server.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const express = require("express") // our express server
const bodyParser = require("body-parser") // requiring the body-parser
const db = require('./database');
const passport = require('./passport');
const cookieParser = require('cookie-parser');
const cookieSession = require('cookie-session');
const session = require('express-session');
const User = require('./models/Users');
const Habits = require('./models/Habits');
const { Schema } = require("./database");
const e = require("express");
const Tracker = require("./models/Tracker");
const Completion = require("./models/Completion");
const moment = require("moment");
const path = require("path");
const cors = require('cors');
const flash = require('connect-flash');
const PORT = process.env.PORT || 3001 // port that the server is running on => localhost:3001
const url = process.env.URL;
const app = express() // generate an app object
const corsOptions = {
origin: 'https://rootine-project.herokuapp.com',
optionsSuccessStatus: 200
}
app.use(
cors({
origin: "*",
methods: 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
credentials: true
})
);
app.use(flash());
// app.options('*', cors());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(session({ cookie: { maxAge: 24 * 60 * 60 * 100},
secret: 'woot',
resave: false,
saveUninitialized: true,
passport: {user: {_id: ''}}
}));
// app.use(
// cookieSession({
// name: "session",
// keys: "woot",
// maxAge: 24 * 60 * 60 * 100
// })
// );
app.use(cookieParser("woot"));
app.use(passport.initialize());
app.use(passport.session());
// // Add headers
// app.use(function (req, res, next) {
// // Website you wish to allow to connect
// res.setHeader('Access-Control-Allow-Origin', '*');
// // Request methods you wish to allow
// res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// // Request headers you wish to allow
// res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
// //Next tells the middleware to move on to the next instruction
// next();
// });
// Serve static files from the React frontend app
app.use(express.static(path.join(__dirname, 'frontend/build')));
app.get('/users', function(req,res){
User.find(function(err, foundUsers) {
if (!err) {
res.send(foundUsers);
} else {
console.log(err);
}
});
});
app.post('/tracker', function(req,res){
Tracker.findOneAndUpdate({date: moment().format('L'), user: req.user.id},
{$set: {"habits.$[el].done": !req.body.done}}, {arrayFilters: [{"el._id": req.body._id}], new: true},
function (err, obj) {
console.log('err', err);
console.log('obj', obj);
});
});
app.get('/tracker', function(req, res){
//TODO: find habits that have that userid
const dayMap = {
'Monday' : 0,
'Tuesday' : 1,
'Wednesday' : 2,
'Thursday' : 3,
'Friday' : 4,
'Saturday' : 5,
'Sunday' : 6,
}
const day = moment().format('dddd');
const dayToIndexFilter = dayMap[day];
Tracker.find({date: moment().format('L'), user: req.user.id}, function(err, foundTracker) {
if(foundTracker.length) {
res.send(foundTracker[0].habits);
} else {
Habits.find({user: req.user.id }, function(err, foundHabits) {
if (!err) {
const filteredHabits = foundHabits.filter(habit => habit.days[dayToIndexFilter]);
const parsedHabits = filteredHabits.map(habit => {
return {
habit_name: habit.habit_name,
done: false,
}
});
Completion.find({user: req.user.id}, function(err, foundCompletion) {
if(!foundCompletion.length) {
newCompletion = new Completion({
user: req.user.id,
dates: [{
month: moment().format('M'),
days: []
}]
});
newCompletion.save();
} else {
Tracker.find({date: moment().subtract(1, "day").format('L'), user: req.user.id}, function(err, foundTracker) {
if(foundTracker.length) {
const foundTrackerHabits = foundTracker[0].habits;
const uncompletedList = foundTrackerHabits.filter(habit => !habit.done);
if (!uncompletedList.length) {
Completion.findOneAndUpdate({user: req.user.id, dates: {$elemMatch: {month : { $gte: moment().format('M')}}}},
{$push: {"dates.$[el].days": moment().subtract(1,"day").format('D')}}, {arrayFilters: [{"el.month": moment().format('M')}], new: true},
function (err, obj) {
console.log("find completion object", obj);
if (!obj) {
Completion.update({user: req.user.id},
{$addToSet: {dates:
{
month:moment().format('M'),
days:[]
}}
}, function(err, obj) {
console.log("err of month not found", err);
console.log('tracker find completion month', obj);
});
}
console.log('err', err);
console.log('tracker find completion obj', obj);
});
}
} else {
Completion.find({user: req.user.id, dates: {$elemMatch: {month : { $gte: moment().format('M')}}}}, function(err, found) {
if (!found.length) {
Completion.update({user: req.user.id},
{$push: {dates:
{
month:moment().format('M'),
days:[]
}}
}, function(err, obj) {
console.log("err of month not found", err);
console.log('tracker find completion month', obj);
});
}
})
};
});
}
})
newTracker = new Tracker({
date: moment().format('L'),
habits: parsedHabits,
user: req.user.id
});
newTracker.save();
res.send(newTracker.habits);
}
console.log(err);
})
}
});
});
app.get('/completion', function(req,res){
Completion.find({user: req.user.id}, function(err, foundCompletions) {
if(!err || foundCompletions) {
res.send(foundCompletions);
} else {
res.redirect('/dashboard');
console.log(err);
}
})
});
app.post('/habits', function(req, res){
const dayMap = {
'Monday' : 0,
'Tuesday' : 1,
'Wednesday' : 2,
'Thursday' : 3,
'Friday' : 4,
'Saturday' : 5,
'Sunday' : 6,
}
const day = moment().format('dddd');
const dayToIndexFilter = dayMap[day];
newHabit = new Habits({
habit_name: req.body.name,
//TODO: need to find a way to have user ID of person logged in to be added to every habit
user: req.user.id,
days: req.body.dates
});
newHabit.save();
if (newHabit.days[dayToIndexFilter]){
const modifyHabit = {
habit_name: newHabit.habit_name,
days: newHabit.days,
done: false
};
Tracker.findOneAndUpdate({date: moment().format('L'), user: req.user.id}, {$push: {habits: modifyHabit}},
function(err, obj) {
console.log('err', err);
console.log('obj', obj);
}
);
}
res.redirect('/dashboard');
});
app.get('/habits', function(req, res){
Habits.find({user: req.user.id }, function(err, foundHabits) {
if (!err) {
res.send(foundHabits);
} else {
console.log(err);
}});
});
app.get('/habits/:habitID', function(req, res) {
const requestedHabitId = req.params.habitID;
Habits.find({_id: requestedHabitId, user: req.user.id}, function(err, habit){
if(!err) {
res.send(habit);
} else {
console.log(err);
}
});
});
app.get('/logout', function(req, res){
req.logout();
res.redirect(`${url}/`);
});
//login via email and password
app.post('/login',
passport.authenticate('local-login', { successRedirect: '/dashboard',
failureRedirect: (`${url}/`),
failureFlash: true })
);
//signup via email and password
app.post('/signup',
passport.authenticate('local-signup', { successRedirect: '/dashboard',
failureRedirect: (`${url}/`),
failureFlash: true,
isLoggedIn: true })
);
app.get('/auth/google',
passport.authenticate('google', { scope: ['profile', 'email'] })
);
app.get('/auth/google/rootine',
passport.authenticate('google'), (err, req, res, next) => { // custom error handler to catch any errors, such as TokenError
if (err.name === 'TokenError') {
res.redirect('/auth/google'); // redirect them back to the login page
} else {
console.log(err);
}
},
(req, res) => { // On success, redirect back to '/'
res.redirect(`${url}/dashboard`);
}
);
app.get('/auth/facebook',
passport.authenticate('facebook', { scope: ['profile', 'email'] }));
app.get('/auth/facebook/rootine',
passport.authenticate('facebook'), (err, req, res, next) => {
if (err.name === 'FacebookTokenError') {
res.redirect('/auth/facebook');
} else {
console.log(err);
}
},
(req, res) => {
res.redirect(`${url}/dashboard`);
}
);
const authCheck = (req, res, next) => {
if (!req.user) {
res.status(401).json({
authenticated: false,
message: "user has not been authenticated"
});
} else {
next();
}
};
// app.get("/dashboard", authCheck, (req, res) => {
// res.status(200).json({
// authenticated: true,
// message: "user is authenticated",
// user: req.user,
// cookies: req.cookies
// });
// });
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'frontend/build', 'index.html'));
});
app.listen(PORT, () => {
// listening on port 3001
console.log(`listening on port ${PORT}`) // print this when the server starts
})