-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
349 lines (294 loc) · 8.27 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
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
// .appId('283596785077054')
// .appSecret('34dd4e09693b2a9ea6cecb3b30bc8f82')
var express = require('express')
, passport = require('passport')
, util = require('util')
, FacebookStrategy = require('passport-facebook').Strategy
, mongoose = require('mongoose')
, db = mongoose.connect('mongodb://localhost/geothon')
, async = require('async')
, schema = require('./schema.js');
var FACEBOOK_APP_ID = "283596785077054"
var FACEBOOK_APP_SECRET = "34dd4e09693b2a9ea6cecb3b30bc8f82";
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: "http://localhost:3000/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);
});
}
));
var app = express();
// configure Express
app.configure(function() {
app.locals.pretty = true;
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
});
app.get('/', function(req, res){
if(!req.user)
res.render('login', { title: 'GeoThon' });
else {
// .find({"created_on": {"$gte": start, "$lt": end}})
var User = db.model('User', schema.user);
User.findOne({fbid: req.user.id}, function(err, docs){
if(err) {
console.log(err);
} else {
req.user.faction = docs.factiune;
req.user.leptoni = docs.leptoni;
var Artefacts = db.model('Artefacts', schema.artefact);
Artefacts.find({}, function(err, docs){
if(err) {
console.log(err);
} else {
res.render('index', {user : req.user, title: 'GeoThon', artefacts: JSON.stringify(docs)});
}
});
}
});
}
});
function callback (err, data) {
if(err) {
console.log(err);
} else {
console.log('got results');
}
}
function getArtefacts (callback) {
var Artefacts = db.model('Artefacts', schema.artefact);
Artefacts.find({}, function(err, docs){
callback(err, docs);
});
}
function getUser (id, callback) {
var User = db.model('User', schema.user);
User.findOne({fbid: id}, function(err, docs){
callback(err, docs);
});
}
app.get('/account', ensureAuthenticated, function(req, res){
res.render('account', { user: req.user, title: 'GeoThon' });
});
app.get('/login', function(req, res){
var schema = mongoose.Schema({
name: String,
facultate: { type: String, default: 'not set' },
munca: { type: String, default: 'not set' },
fbid: { type: String, default: 'not set' },
leptoni: { type: Number, default: 10 },
artefacte: Array,
bio: { type: String, default: 'not set' },
factiune: {type: String, default: 'not set'}
});
var User = db.model('User', schema);
User.findOne({fbid: req.user.id}, function(err, doc){
if(err) {
console.log(err);
res.end();
} else {
req.user.faction = doc.factiune;
res.render('login', { user: req.user, title: 'GeoThon' });
}
});
});
app.get('/add', function(req, res){
read = require('fs').createReadStream;
stream = read('/opt/apps/rpg/server/artifacts.txt');
var schema = mongoose.Schema({
name: String,
descriere: { type: String, default: 'not set' },
lat: Number,
lng: Number,
pret: { type: Number, default: 10 },
owner: String,
bids: [],
radius: Number
});
var Artefacts = db.model('Artefacts', schema);
var content = '';
stream.on('data', function(data){
content+=data;
});
stream.on('error', function(err){
console.log(err);
});
stream.on('end', function(){
console.log(content);
var json = JSON.parse(content);
json.forEach(function(a){
console.log(a);
var artefact = new Artefacts({
name: a.name,
lat: a.lat,
lng: a.lng,
pret: 1000,
radius: 100
});
artefact.save(function(err){
if(err) {
console.log(err);
} else {
console.log('inserted');
}
});
});
res.end(content);
});
});
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.
});
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/login' }),
function(req, res) {
var schema = mongoose.Schema({
name: String,
facultate: { type: String, default: 'not set' },
munca: { type: String, default: 'not set' },
fbid: { type: String, default: 'not set' },
leptoni: { type: Number, default: 10 },
artefacte: Array,
bio: { type: String, default: 'not set' },
factiune: {type: String, default: 'not set'}
});
var User = db.model('User', schema);
User.findOne({fbid: req.user.id}, function(err, doc){
if(err) {
console.log(err);
res.redirect('/');
} else {
if(doc) {
console.log('user found');
req.user.faction = doc.factiune;
console.log(req.user.faction, doc.factiune);
res.redirect('/');
} else {
var user = new User({
name: req.user.displayName,
bio: req.user._json.bio,
fbid: req.user.id
});
user.save(function(err){
if(err) {
console.log(err);
res.redirect('/');
} else {
console.log('user saved');
req.user.faction = 'not set';
res.redirect('/');
}
});
}
}
});
});
app.get('/bid/:artefact/:user/:sum/:name', function(req, res){
var artefact = req.params.artefact;
var id = req.params.user;
var sum = req.params.sum;
var name = req.params.name;
console.log(artefact, id, sum, name);
var Artefacts = db.model('Artefacts', schema.artefact);
Artefacts.findOne({name: artefact}, function(err, doc){
if(err) {
console.log(err);
} else {
var found = 0;
doc.bids.forEach(function(bid, idx){
if(bid.id == id) {
console.log('bid found');
console.log(doc.bids[idx]);
doc.bids[idx].sum = parseInt(doc.bids[idx].sum) + parseInt(sum);
found = 1;
console.log(doc.bids[idx]);
}
});
if(!found)
doc.bids.push({
id: id,
sum: sum,
name: name
});
Artefacts.findOneAndUpdate({name: artefact}, {bids: doc.bids}, function(err){
if(err) {
console.log(err);
} else {
console.log('bid saved');
res.end('bid saved');
}
});
}
});
});
app.get('/updatesum/:sum/:id', function(req, res){
var User = db.model('User', schema.user);
User.findOneAndUpdate({fbid: req.params.id}, {leptoni: parseInt(req.params.sum)}, function(err){
if(err) {
console.log(err);
} else {
console.log('sum updated');
}
});
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
app.get('/setfaction/:user/:faction', function(req, res){
console.log(req.params.user, req.params.faction);
var schema = mongoose.Schema({
name: String,
facultate: { type: String, default: 'not set' },
munca: { type: String, default: 'not set' },
fbid: { type: String, default: 'not set' },
leptoni: { type: Number, default: 10 },
artefacte: Array,
bio: { type: String, default: 'not set' },
factiune: {type: String, default: 'not set'}
});
var User = db.model('User', schema);
User.findOneAndUpdate({fbid: req.params.user}, {factiune: req.params.faction}, function(err){
if(err) {
console.log(err);
}
});
res.end('Faction set');
});
app.listen(3000);
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login')
};