-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
51 lines (44 loc) · 1.62 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
var express = require('express'),
app = express(),
passport = require('passport'),
TwitterStrategy = require('passport-twitter').Strategy;
app.configure(function() {
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.set('hostname', 'localhost');
app.set('port', 4000);
app.set('TWITTER_CONSUMER_KEY', process.env.TWITTER_CONSUMER_KEY);
app.set('TWITTER_CONSUMER_SECRET', process.env.TWITTER_CONSUMER_SECRET);
});
passport.use(new TwitterStrategy({
consumerKey: app.get('TWITTER_CONSUMER_KEY'),
consumerSecret: app.get('TWITTER_CONSUMER_SECRET'),
callbackURL: 'http://' + app.get('hostname') + ':' + app.get('port') + '/auth/twitter/callback'
},
function(token, tokenSecret, profile, done) {
done(null, profile);
}
));
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
app.get('/', function(req, res){
if(req.user){
res.send(req.user);
} else {
res.send('<a href="/auth/twitter">Sign in with Twitter</a>');
}
});
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback',
passport.authenticate('twitter', { successRedirect: '/',
failureRedirect: '/login' }));
console.log('App running, head to http://' + app.get('hostname') + ':' + app.get('port') + ' to sign in with Twitter');
app.listen(app.get('port'));