-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
118 lines (103 loc) · 2.88 KB
/
index.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
if (process.env.NODE_ENV === "development") {
require('dotenv').config();
}
var express = require('express');
var cors = require('cors');
var passport = require('passport');
var Strategy = require('passport-twitter').Strategy;
var TwitterClient = require('twitter');
var config = require('./config')
passport.use(new Strategy({
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
callbackURL: config.tweetsServerUrl + '/login/return'
},
function(token, tokenSecret, profile, cb) {
profile._token = token
profile._tokenSecret = tokenSecret
return cb(null, profile);
}));
passport.serializeUser(function(user, cb) {
cb(null, user);
});
passport.deserializeUser(function(obj, cb) {
cb(null, obj);
});
var app = express();
if (process.env.NODE_ENV === "development") {
// app.use(require('morgan')('combined'));
}
app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
var whitelist = [
'https://tweets.now.sh',
'https://tweets-dev.now.sh',
'https://tweets-next.now.sh'
];
if (process.env.NODE_ENV === "development") {
whitelist.push('http://localhost:5000');
}
var corsOptions = {
origin: function (origin, callback) {
if (origin) {
if (whitelist.indexOf(origin) === -1) {
callback(new Error('Access not allowed'))
} else {
callback(null, true);
}
} else {
callback(null, true);
}
},
credentials: true
}
app.use(cors(corsOptions))
app.use(passport.initialize());
app.use(passport.session());
app.get('/login',
passport.authenticate('twitter'));
app.get('/login/return',
passport.authenticate('twitter', { failureRedirect: config.tweetsUrl }),
function(req, res) {
res.redirect(config.tweetsUrl);
});
app.get('/logout',
function(req, res){
req.logout();
res.redirect(config.tweetsUrl);
});
app.get('/tweets/:id*?',
function(req, res){
var user = req.user;
if (user && user._token && user._tokenSecret) {
var Twitter = new TwitterClient({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token_key: req.user._token,
access_token_secret: req.user._tokenSecret
});
var params = {
count: 800
};
var id = req.params.id;
if (id > 0) {
params.max_id = id;
}
Twitter.get('statuses/home_timeline', params, function(error, tweets, response) {
if(error) {
if(res.headersSent) {
res.send(error);
} else {
res.status(400).send(error);
}
}
res.send(tweets);
});
} else {
res.status(401).send([]);
}
});
app.listen(5001, function () {
console.log('tweets-server started successfully');
});