-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
59 lines (49 loc) · 1.67 KB
/
auth.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
var express = require('express')
, passport = require('passport')
, Strategy = require('passport-twitter').Strategy
, open = require('open')
, fs = require('fs')
, config = require(__dirname + '/config.json')
, port = process.env.PORT || 3000;
passport.use(new Strategy({
consumerKey: config.consumerKey,
consumerSecret: config.consumerSecret,
callbackURL: "http://127.0.0.1:3000/login/twitter/return"
},
function(token, tokenSecret, profile, done) {
var tokenData = {
token: token,
tokenSecret: tokenSecret
};
fs.writeFileSync(__dirname + '/token.json', JSON.stringify(tokenData) ,'utf-8');
done(null, profile);
}
));
passport.serializeUser(function(user, cb) {
cb(null, user);
});
passport.deserializeUser(function(obj, cb) {
cb(null, obj);
});
var app = express();
// 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 }));
app.use(passport.initialize());
app.use(passport.session());
app.get('/', passport.authenticate('twitter'));
app.get('/error', function(req, res) {
res.send('Ops, algo deu errado!');
});
app.get('/login/twitter/return',
passport.authenticate('twitter', { failureRedirect: '/error' }),
function(req, res) {
res.send('Pode fechar esta janela agora! :)');
console.log('Tudo pronto! Agora é só rodar:\n\nnode fetch.js <tweet_id>\nnode sorteia.js');
setTimeout(process.exit, 1000);
});
app.listen(port, function() {
console.log('App rodando na porta '+port);
});
open('http://127.0.0.1:'+port+'/');