forked from shampliu/sbhacks-delivr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
217 lines (180 loc) · 6.61 KB
/
web.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
var express = require('express')
, passport = require('passport')
, util = require('util')
, FacebookStrategy = require('passport-facebook').Strategy
, session = require('express-session')
, bodyParser = require("body-parser")
, cookieParser = require("cookie-parser")
, methodOverride = require('method-override')
, mongoskin = require('mongoskin');
var logfmt = require("logfmt");
var mongo = require('mongodb');
var path = require('path');
var FACEBOOK_APP_ID = "1396800567290730"
var FACEBOOK_APP_SECRET = "c6a5a80fce3f0856e247661f36a3678e";
var app = express();
// configure Express
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(cookieParser());
app.use(bodyParser());
app.use(methodOverride());
app.use(session({ secret: 'keyboard cat' }));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(__dirname + '/public'));
// MongoDB
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost:27017/collectionName';
var db = mongoskin.db(mongoUri, {safe:true});
app.param('collectionName', function(req, res, next, collectionName){
req.collection = db.collection(collectionName)
return next()
})
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing. However, since this example does not
// have a database of user records, the complete Facebook profile is serialized
// and deserialized.
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// Use the FacebookStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and Facebook
// profile), and invoke a callback with a user object.
passport.use(new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: "/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// To keep the example simple, the user's Facebook profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the Facebook account with a user record in your database,
// and return that user instead.
return done(null, profile);
});
}
));
app.get('/', function(req, res){
res.render('index', { user: req.user });
});
app.get('/account', ensureAuthenticated, function(req, res){
res.render('account', { user: req.user });
});
app.get('/login', function(req, res){
res.render('login', { user: req.user });
});
app.get('/feed', function(req, res) {
res.render('feed.ejs', { user : req.user });
})
app.post('/post', function(req, res){
//if (not session variable - logged into facebook) -> print out that your session is logged out
//else -> run below
var col = db.collection("userPosts")
var temp = req.body
temp["TimeOfPost"] = new Date()
col.insert(req.body, {}, function(e,results){
if (e){
res.status(500).send()
}
else{
res.send(results)
}
});
});
app.put('/pickedUp', function(req, res) {
var col = db.collection("userPosts");
// console.log(req.body.Post_id + "FUCKKKKK YESSSSSSS");
// col.update({ _id: ObjectId(req.body.Post_id) }, {'$pull':{ PickedUp:"false"}}, function(err) {
// if (err) throw err;
// col.update({ _id: req.body.Post_id }, {'$push':{ PickedUp:"true" }}, function(err) {
// if (err) throw err;
// console.log('Updated!');
// });
// });
console.log("yes");
col.updateById(req.body.Post_id, { PickedUp: "true" }, function(err) {});
col.updateById(req.body.Post_id, { $set: {"PickedUp":'true'}}, function(err) {});
// res.send('success!!');
// col.update({ _id : ObjectId(req.body.Post_id)}, {$set:{PickedUp:"true"}}, function(err, result) {
// if (!err) console.log('PickedUp updated!');
// });
});
app.post('/deliveryInfo', function(req, res){
var col = db.collection("deliveryInfo")
col.insert(req.body, {}, function(e, results){
if (e){
res.status(500).send()
}
else{
console.log(req.body);
res.send(req.body);
}
});
});
app.get('/getDeliveryInfo', function(req,res){
var col = db.collection("deliveryInfo")
col.find().toArray(function(e, results){
res.send(results);
});
});
app.get('/newsfeed', function(req,res){
var col = db.collection("userPosts")
col.find().toArray(function(e, results){
res.send(results);
});
});
// GET /auth/facebook
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in Facebook authentication will involve
// redirecting the user to facebook.com. After authorization, Facebook will
// redirect the user back to this application at /auth/facebook/callback
app.get('/auth/facebook',
passport.authenticate('facebook'),
function(req, res){
// The request will be redirected to Facebook for authentication, so this
// function will not be called.
});
// GET /auth/facebook/callback
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
app.delete('/delete/post', function(req, res) {
var collection = db.collection("userPosts")
collection.removeById(req.body.theid, function(e, result){
res.send(result)
})
})
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {});
// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login')
}