-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb.js
95 lines (84 loc) · 3.07 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
// load dependencies
var express = require('express')
, http = require('http')
, path = require('path');
var election = require('./routes/election')
, auth = require('./routes/auth');
var login = require('./helpers/login');
var passport = require('passport')
, NYUPassportStrategy = require('passport-nyu').Strategy;
// prepare database
var mongoose = require('mongoose');
mongoose.connect(process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/voting');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
// schema for users
var Election = require('./models/election');
var User = require('./models/user');
});
// start app server
var app = express();
// configure express
app.configure(function(){
app.set('port', process.env.PORT || 5000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser( process.env.SECRET ));
app.use(express.session({ key: 'voting.sess', secret: process.env.SECRET }));
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'));
});
// --- development
app.configure('development', function(){
app.use(express.errorHandler());
});
// all routes
app.get('/', election.info);
app.get('/elections', [login.ensure, election.list]);
app.get('/election/new', [login.ensure, election.new]);
app.get('/election/:id/vote', login.ensure, election.view);
// app.get('/election/:id/vote', [login.ensure, election.view]);
app.get('/election/:id/admin', [login.ensure, election.admin]);
app.post('/election/:id/vote', [login.ensure, election.vote]);
app.post('/election/:id/admin', election.vote);
// app.get('/election/:election/vote/:race/for/:candidate', election.vote);
// authentication with passport
passport.serializeUser(function(user, done) {
done(null, JSON.stringify(user));
});
passport.deserializeUser(function(user, done) {
done(null, JSON.parse( user ) );
});
// oauth
passport.use('nyu-passport', new NYUPassportStrategy({
clientID: process.env.PASSPORT_ID,
clientSecret: process.env.PASSPORT_SECRET,
callbackURL: process.env.BASE_URL + '/auth/passport/callback'
},
function(accessToken, refreshToken, profile, done) {
user = {
token: accessToken,
netID: profile.netID,
class: profile.class
};
done(null, user);
}
));
// passport auth
app.get('/auth/start', auth.start); // start the auth process
app.get('/auth/passport', passport.authenticate('nyu-passport')); // pass along to passport
app.get('/auth/passport/callback', passport.authenticate('nyu-passport', { successRedirect: '/auth/end', failureRedirect: '/auth/passport' })); // hear back from Passport
app.get('/auth/end', auth.finish); // finish the auth process
// start listening
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});