-
Notifications
You must be signed in to change notification settings - Fork 1
/
passport-server.js
130 lines (102 loc) · 3.86 KB
/
passport-server.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
var express = require('express')
, passport = require('passport')
, util = require('util')
, FB = require('fb');
var FacebookStrategy = require('passport-facebook').Strategy;
var FACEBOOK_APP_ID = '610602015680966';
var FACEBOOK_APP_SECRET = '1a0eb427f36d45a0e81e3bb52c7a1245';
var app = express();
// Path module -- setting up static files
var path = require('path'); // Using path module to easily serve static files and favicon
app.use(express.static(path.join(__dirname, 'public'))); // Tell Express where to find static JS and CSS files
app.use(express.favicon(path.join(__dirname, 'public/images/favicon.ico'))); // Tell Express where to find favicon
app.use(express.bodyParser());
// Setting up Templating Engine
var engines = require('consolidate');
app.engine('html', engines.hogan); // Tell Express to run .html files through Hogan
app.set('views', __dirname + '/views'); // Tell Express where to find templates
// ========== Passport START ==========
app.configure(function() {
// Initialize passport! (use passport.session middleware)
/// for persistent login sessions
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});
// Passport session steup
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
var FB_ACCESS_TOKEN = '';
/* profileFields INFO:
Portable Contacts schema for profileFields
http://wiki.portablecontacts.net/w/page/17776141/schema
You also need permissions in permissions=[] array below.*/
// Use FacebookStrategy
passport.use(new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: "http://localhost:8080/auth/facebook/callback"
// profileFields: ['id', 'name', 'displayName', 'photos']
},
function(accessToken, refreshToken, profile, done) {
process.nextTick(function() {
FB_ACCESS_TOKEN = accessToken;
FB.setAccessToken(accessToken);
return done(null, profile);
});
}
));
// =========== Passport END =============
// =========== Express START ============
app.get('/', function(req, res) {
/////
console.log('Successfully redirected to / after logging in!');
res.render('index.html');
});
// ==================== TEST ================
app.get('/name', function(req, res) {
FB.setAccessToken(FB_ACCESS_TOKEN);
console.log("FB_ACCESS_TOKEN: " + FB_ACCESS_TOKEN);
FB.api('/me/photos', function(res) {
console.log("/photos: " + res.source + ", " + res.url + ", " + res.message);
console.log('/name RESPONSE: ' + JSON.stringify(res));
});
/* var body = "Hello World!";
FB.api('/me/feed', 'post', {message: body}, function(response) {
if (!response || response.error) {
console.log("Error occurred: " + JSON.stringify(response.error));
} else {
console.log("Posted status update: " + body);
console.log("POST ID: " + response.id);
}
});*/
})
// =========================================
// Redirects to facebook.com/login
var permissions = ['user_status', 'user_about_me', 'user_likes',
'user_photos', 'publish_actions', 'read_stream'];
app.get('/auth/facebook',
passport.authenticate('facebook', /*{display: 'popup'}, */{scope: permissions }),
function(req, res) {
});
// Gets called after user logins from facebook.com
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {failureRedirect:'/login'}),
function(req, res) {
res.redirect('/');
});
app.listen(8080);
// =========== Express END ===========
// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticated(req, res, callback) {
if (req.isAuthenticated()) { return callback(); }
res.redirect('/login')
}