-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb.js
93 lines (81 loc) · 2.75 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
// load dependencies
var express = require('express')
, http = require('http')
, path = require('path');
var project = require('./routes/project')
, documents = require('./routes/documents');
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/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
// schema for users
var Project = require('./models/project');
});
// 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: 'status.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('/', documents.index);
app.get('/projects', project.list);
// app.get('/projects/edit', project.edit);
app.post('/project/:slug/update', project.update);
app.get('/document/:slug', documents.view)
// authentication with passport
passport.serializeUser(function(user, done) {
done(null, user.token);
});
passport.deserializeUser(function(token, done) {
done(null, token);
});
// oauth
passport.use('nyu-passport', new NYUPassportStrategy({
clientID: process.env.PASSPORT_ID,
clientSecret: process.env.PASSPORT_SECRET,
callbackURL: process.env.BASE_URL + '/auth/provider/callback'
},
function(accessToken, refreshToken, profile, done) {
console.log( profile );
user = {
token: accessToken,
netID: profile.netID
}
done(null, user);
}
));
// google auth
app.get('/auth/provider', passport.authenticate('nyu-passport'));
// The OAuth 2.0 provider has redirected the user back to the application.
// Finish the authentication process by attempting to obtain an access
// token. If authorization was granted, the user will be logged in.
// Otherwise, authentication has failed.
app.get('/auth/provider/callback',
passport.authenticate('nyu-passport', { successRedirect: '/', failureRedirect: '/auth/provider' }));
// start listening
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});