forked from tglines/nodrr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
101 lines (87 loc) · 2.66 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
express = require('express');
force_domain = require('connect-force-domain');
app = module.exports = express.createServer(force_domain('www.nodrr.com'));
sys = require('sys');
fs = require('fs');
http = require('http');
mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/db');
Schema = mongoose.Schema;
ObjectId = Schema.ObjectId;
require('./models/account.js');
require('./models/nod.js');
Account = mongoose.model('Account');
Nod = mongoose.model('Nod');
connect = require('connect');
auth = require('connect-auth');
require('./fb_creds.js');
//mongoStore = require('connect-mongodb');
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
//store: mongoStore({
// dbname: 'sessions',
// username: '',
// password: ''
// }),
secret: 'nodrrsecret'
}));
app.use(express.logger({ format: ':date :remote-addr :method :status :url' }));
app.use(auth([
auth.Facebook({appId : fbId, appSecret: fbSecret, scope : "email", callback: fbCallbackAddress})
]));
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
var loadFacebookAccount = function(facebook_details,loadCallback){
Account.findOne({ facebook_id: facebook_details.user.id }, function(err,account){
if(account){
loadCallback(account);
}
else{
var n = new Account();
n.email = facebook_details.user.email;
n.type = 1;
n.facebook_id = facebook_details.user.id;
n.date = new Date();
n.save(function(err){
loadCallback(n);
});
}
});
}
loadAccount = function(req,loadCallback){
if(req.isAuthenticated()){
//load account out of database
if(req.getAuthDetails().user.id){
//its a facebook login - try and grab out of db otherwise make a user off of fbook credentials
var fbook_details = req.getAuthDetails();
loadFacebookAccount(fbook_details,loadCallback);
}
}
else{
loadCallback(null);
}
}
require('./global_funcs.js');
// Routes ( Controllers )
require('./controllers/home.js');
require('./controllers/auth.js');
require('./controllers/edit.js');
require('./controllers/nod.js');
require('./controllers/user.js');
//Only listen on $ node app.js
if (!module.parent) {
app.listen(80);
console.log("Express server listening on port %d", app.address().port)
}