-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
70 lines (56 loc) · 1.57 KB
/
main.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
var express = require("express");
var logfmt = require("logfmt");
var passport = require('passport');
var flash = require('connect-flash');
var app = express();
var CouchDB = require( 'promised-couch' ).CouchDB;
var db = CouchDB( { base: process.env.DBURL } );
// used to serialize the user for the session
passport.serializeUser(function(user, cb) {
console.log('user', user);
cb(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function(id, cb) {
db.get(id).then(function(user) {
cb(null, user);
}, function(err) {
cb(err);
});
});
app.use(logfmt.requestLogger());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.set('view engine', 'ejs');
// required for passport
app.use(express.session({ secret: 'eukjhfudhbfdb' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
// configure passport for twitter
require('./auth/twitter')(app, db, passport);
app.get('/', function(req, res) {
db.get('welcome').then(function(doc) {
res.render('index.ejs', doc);
}, function (err) {
res.send(503, err.toString());
});
});
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/');
}
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile.ejs', {
user : req.user
});
});
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
console.log("Listening on " + port);
});