-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
executable file
·150 lines (130 loc) · 4.29 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//dependencies for each module used
var express = require('express');
var http = require('http');
var path = require('path');
var handlebars = require('express3-handlebars');
var app = express();
//load environment variables
var dotenv = require('dotenv');
dotenv.load();
//fbgraph
var graph = require('fbgraph');
//twit
var twit = require('twit');
//twitter oauth
var passport = require('passport');
var util = require('util');
var passportTwitterStrategy = require('passport-twitter').Strategy;
//have two blank strings for access token and access secret
var accessToken = "";
var accessSecret = "";
var twitterOauth = {
consumer_key: process.env.twitter_client_id,
consumer_secret: process.env.twitter_client_secret,
access_token: accessToken,
access_token_secret: accessSecret
};
//Set up passport session set up.
//This allows persistant login sessions so the user doesn't need to keep logging in everytime
//for their access token
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// Simple route middleware to ensure user is authenticated.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/');
}
//Use TwitterStrategy with passport
passport.use(new passportTwitterStrategy({
consumerKey: process.env.twitter_client_id,
consumerSecret: process.env.twitter_client_secret,
callbackURL: "http://localhost:3000/auth/twitter/callback"
}, function (token, tokenSecret, profile, done) {
//setting up access token
accessToken = token;
accessSecret = tokenSecret;
twitterOauth.access_token = token;
twitterOauth.access_token_secret = tokenSecret;
//Continuing on
process.nextTick(function() {
return done(null, profile);
});
}));
//Configures the Template engine
app.engine('handlebars', handlebars());
app.set('view engine', 'handlebars');
app.set('views', __dirname + '/views');
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.bodyParser());
//more setting up configuration for express
//Allows cookie access and interaction
app.use(express.cookieParser() );
app.use(express.session({ secret: 'nyan cat'}));
//Intialize passport
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
//routes
app.get('/', function(req,res) {
res.render("index");
});
//fbgraph authentication
app.get('/auth/facebook', function(req, res) {
if (!req.query.code) {
var authUrl = graph.getOauthUrl({
'client_id': process.env.facebook_client_id,
'redirect_uri': 'http://localhost:3000/auth/facebook',
'scope': 'user_about_me'//you want to update scope to what you want in your app
});
if (!req.query.error) {
res.redirect(authUrl);
} else {
res.send('access denied');
}
return;
}
graph.authorize({
'client_id': process.env.facebook_client_id,
'redirect_uri': 'http://localhost:3000/auth/facebook',
'client_secret': process.env.facebook_client_secret,
'code': req.query.code
}, function( err, facebookRes) {
res.redirect('/UserHasLoggedIn');
});
});
app.get('/UserHasLoggedIn', function(req, res) {
graph.get('me', function(err, response) {
console.log(err); //if there is an error this will return a value
data = { facebookData: response};
res.render('facebook', data);
});
});
//twitter authentication Oauth setup
//this will set up twitter oauth for any user not just one
app.get('/auth/twitter', passport.authenticate('twitter'), function(req, res) {
//nothing here because callback redirects to /auth/twitter/callback
});
//callback. authenticates again and then goes to twitter
app.get('/auth/twitter/callback',
passport.authenticate('twitter', { failureRedirect: '/' }),
function(req, res) {
res.redirect('/twitter');
});
app.get('/twitter', ensureAuthenticated, function(req, res) {
//I can use twitterOauth as previously it's an array set up with the correcet information
var T = new twit(twitterOauth);
T.get('/friends/list', function (err, reply) {
console.log(err); //If there is an error this will return a value
data = { twitterData: reply };
res.render('twitter', data);
});
});
//set environment ports and start application
app.set('port', process.env.PORT || 3000);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});