forked from scrmdgmilynr/nomad
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
356 lines (302 loc) · 9.47 KB
/
Copy pathserver.js
File metadata and controls
356 lines (302 loc) · 9.47 KB
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
350
351
352
353
354
355
356
'use strict';
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const multer = require('multer');
const passport = require('passport');
const session = require('express-session');
const upload = multer();
const path = require('path');
const flash = require('connect-flash');
const cookieParser = require('cookie-parser');
const LocalStrategy = require('passport-local').Strategy;
const FacebookStrategy = require('passport-facebook').Strategy;
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
const _ = require('underscore');
const dateFormat = require('dateformat');
const app = express();
const bcrypt = require('bcrypt-nodejs');
const pool = require('./db/postgresConnect.js'); //database
const port = process.env.PORT || 5000; // port
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(express.static(path.join(__dirname, '/client'))); // static files
// -> Passport Middleware Setup <- //
app.use(session({secret: 'mySecretKey',
resave: true,
saveUninstalized: true,
cookie: { secure: true,
maxAge: 60000}
})); //session secret key
app.use(passport.initialize()); //initialize passport
app.use(passport.session()); // initialize session
app.use(cookieParser());
app.use(flash());
passport.use('login', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(req, email, password, done){
// query based on the email
console.log('request:' , req);
var isValidPassword = function(user, password){
console.log('49 - Password: ', password);
console.log('50 - user.password: ', user)
return bcrypt.compareSync(user, password);
}
pool.query('SELECT * from public.users where email=$1', [email], function(err, results){
// console.log('hash: ', results.rows[0].password);
var user = {id: results.rows[0].id}
if(err){
console.log('line 54: ', err);
return done(err);
}
if(results.rows.length === 0){
console.log('User Not Found with username '+ email);
return done(null, false, req.flash('message', 'User Not found.'));
}
if(!isValidPassword(password, results.rows[0].password)){
console.log('Invalid Password');
return done(null, false, {message: "Incorrect password"});
// req.flash('message', 'Invalid Password');
} else {
return done(null, user);
}
})
}
));
passport.use(new GoogleStrategy({
clientID: '522655539394-stb3c3vkcaibnkg8nqt8e1brf1l16pg6.apps.googleusercontent.com',
clientSecret: 'nhD_2iN0w-oqydEUhCQWSyR1',
callbackURL: "http://localhost:5000/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
console.log(profile);
done(null, profile);
}
));
passport.serializeUser(function(user, done) {
console.log("user", user)
done(null, user);
});
passport.deserializeUser(function(user, done) {
// User.findById(id, function(err, user) {
done(null, user);
// });
});
passport.use( new FacebookStrategy({
clientID: '618793941624094',
clientSecret: 'dfcf86bf3f9e3db4876e9a3ded63075f',
callbackURL: "http://localhost:5000/auth/facebook/callback",
profileFields: ['id', 'displayName', 'photos', 'email']
},
function(accessToken, refreshToken, profile, done) {
console.log('profile', profile);
console.log(accessToken, 'access');
console.log(refreshToken, 'refresh')
done(null, profile.displayName)
}
));
app.post('/login', passport.authenticate('login'), function(req, res, next) {
// res.send({ body: res.body,
// user: req.user });
res.send(req.user);
})
app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/google',
passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/plus.login'] }));
app.get('/profile', function(req,res){
console.log('redirecting...')
res.end()
})
app.get('/auth/facebook/callback', passport.authenticate('facebook', {failureRedirect: '/'}),
function(req,res){
console.log('userAuthentication: ', req.isAuthenticated())
res.redirect('/#/profile')
});
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/' }),
function(req, res) {
res.redirect('/#/profile');
});
app.post('/api/getEvent', (req,res) =>{
//data is an array
const structure = (data) =>{
let newData = data;
let times = _.reduce(data, (final, event) =>{
final = final || {};
const key = `${event.lat},${event.long},${event.name}`
//format times to human readable
const newStart = dateFormat(new Date(event.start_date), "mmm dS, yy, h:MM TT")
const newEnd = dateFormat(new Date(event.end_date), "mmm dS, yy, h:MM TT")
const timeObj = {
start: newStart,
end: newEnd
};
if(final[key] === undefined){
final[key] = {check: false};
// console.log(new Date(event.start_date), new Date(event.end_date));
final[key].times = [timeObj];
}else{
final[key].times.push(timeObj);
}
return final;
}, {});
let count = 1;
return _.chain(newData).map((event) =>{
const key = `${event.lat},${event.long},${event.name}`;
if(times[key].check === false){
event.time = times[key].times;
times[key].check = true;
delete event.start_date;
delete event.end_date;
}
return event;
}).filter((event) =>{
return event.time !== undefined || null;
}).map((event) =>{
if(count % 2 === 0){
event.other = 'color';
}else{
event.other = 'notcolor';
}
count++;
return event;
});
};
new Promise((resolve, reject) =>{
pool.connect(function(err, client, done) {
if(err) {
reject(err);
}
resolve(client);
});
}).then((client) => {
client.query(`select public.events.id, name, host, description, paypal, lat, long, start_date, end_date
from public.events inner join public.dates on public.dates.fk_event = public.events.id
where acos(sin(${req.body.lat}) * sin(public.events.lat) +
cos(${req.body.lat}) * cos(public.events.lat) *
cos(public.events.long - (${req.body.long}))) * 6371 <= ${req.body.radius} and
to_timestamp(public.dates.start_date, 'YYYY-MM-DD') >= now() order by public.events.id;`,
(err, result)=>{
const events = {
events: structure(result.rows)
};
res.send(events).end();
}
);
});
})
app.get('/api/userEvents', (req,res) =>{
client.query(`select * from public.events where userid='${req.body.id})'`)
.on('row',function(row){
console.log(row);
});
})
app.post('/api/createEvent', (req, res) =>{
console.log(JSON.stringify(req.body, null, 3));
//turn lat and long into radians
req.body.location.lat = req.body.location.lat.toRad();
req.body.location.long = req.body.location.long.toRad();
console.log(req.body)
let insertTimes = (client, cb) =>{
_.each(req.body.time, (time) =>{
console.log(req.body.info.name, req.body.info.location);
client.query(`insert into public.dates
(start_date, end_date, fk_event)
values ('${time.start}',
'${time.end}',
(select id from public.events where name='${req.body.info.name}' and lat=${req.body.location.lat} and long=${req.body.location.long}))`,
(err, result) =>{
console.log(err, 'check error');
console.log(result, ' result from insert statement');
}
);
});
cb();
};
// insert into public.events (name, host, description, lat, long) values(,,,,,,);
new Promise((resolve, reject) =>{
pool.connect(function(err, client, done) {
if(err) {
reject(err);
}
resolve(client);
});
}).then((client) =>{
client.query(`insert into public.events
(name, host, description, lat, long, paypal)
values ('${req.body.info.name}',
'${req.body.info.host}',
'${req.body.info.description}',
${req.body.location.lat},
${req.body.location.long},
'${req.body.info.paypal}')`,
(err, result) =>{
console.log(err, 'check error');
console.log(result, ' result from insert statement');
insertTimes(client, () => {
res.send();
});
}
);
});
});
app.post('/api/createUser', function(req,res){
var createHash = function(password){
return bcrypt.hashSync(password, bcrypt.genSaltSync(10));
}
req.body.userinfo.password = createHash(req.body.userinfo.password);
new Promise((resolve, reject) =>{
pool.connect(function(err, client, done) {
if(err) {
reject(err);
}
resolve(client);
})
}).then((client) =>{
client.query(`insert into public.users
(name, email, password)
values ('${req.body.userinfo.name}',
'${req.body.userinfo.email}',
'${req.body.userinfo.password}')`,
(err, result) =>{
console.log(err, 'check error')
console.log(result, ' result from insert statement')
res.send();
}
);
});
})
app.listen(port, () =>{
// listening
});
// posting an event has a request body like so
/*
{
info: {
name:,
host:,
description:
},
location: {
lat:,
long:
}
time:[
{start:, end:} // ISO FROMAT
]
}
*/
/*
function to help make seed data
var latlong = function(lat, long) {
var latRad = lat.toRad();
var longRad = long.toRad();
return `${latRad}, ${longRad}`;
}
*/