-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
125 lines (106 loc) · 3.5 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
var _ = require('lodash'),
express = require('express'),
cons = require('consolidate'),
fs = require('fs'),
moment = require('moment'),
swig = require('swig'),
passport = require('passport'),
util = require('util'),
PocketStrategy = require('./pocket-strategy-passport.js'),
config = require('./config.json');;
// Pocket App token
POCKET_CONSUMER_KEY = config.key;
if (POCKET_CONSUMER_KEY === "Pocket consumer key") {
console.log('WARNING!!! Need a pocket costumer key');
}
// Passport Set serializers
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// Passport Set up
var pocketStrategy = new PocketStrategy({
consumerKey: POCKET_CONSUMER_KEY,
callbackURL: "http://127.0.0.1:3000/auth/pocket/callback"
}, function(username, accessToken, done) {
process.nextTick(function() {
return done(null, {
username: username,
accessToken: accessToken
});
});
});
passport.use(pocketStrategy);
// Express set up
var server = express();
server.configure(function() {
server.use(express.logger());
server.use(express.cookieParser());
server.use(express.bodyParser());
server.use(express.methodOverride());
server.use(express.session({
secret: 'gato goethe kappa nabo'
}));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
server.use(passport.initialize());
server.use(passport.session());
server.use(server.router);
server.use('/twbs', express.static(__dirname + '/bower_components/bootstrap/dist/css'));
server.use('/data', express.static(__dirname + '/data'));
});
swig.init({
root: './views',
cache: false
});
server.use(express.static('./public'));
server.engine('.html', cons.swig);
server.set('view engine', 'html');
server.set('views', './views');
server.get('/', function(req, res) {
console.log('Req to / by ' + (req.user ? req.user.username : 'unknown'));
if (req.user) {
var jsonDownloaded = fs.existsSync(__dirname + '/data/' + req.user.username + '.json');
if (jsonDownloaded) {
res.render('index', {
user: req.user
});
} else {
pocketStrategy.getAllItems(req.user.accessToken, function(err, items) {
if (err) {
res.send('Something went wrong');
return;
}
console.log(__dirname + '/data/' + req.user.username + '.json');
fs.writeFileSync(__dirname + '/data/' + req.user.username + '.json', JSON.stringify(_.values(items.list)));
res.render('index', {
user: req.user
});
});
}
} else {
res.render('index', {
user: req.user
});
}
});
// Passport routes for express
server.get('/auth/pocket', passport.authenticate('pocket'),
function(req, res) {
// If user is already log in and this url is called please readirect the user to the correct place.
res.redirect('/');
});
server.get('/auth/pocket/callback', passport.authenticate('pocket', {
failureRedirect: '/login'
}),
function(req, res) {
res.redirect('/');
});
server.get('/logout', function(req, res) {
req.session.destroy();
res.redirect('/');
});
server.listen(3000);
console.log('server running at : http://127.0.0.1:3000')