Skip to content

Commit fd64e2a

Browse files
committed
Passport Facebook
1 parent 3b5483b commit fd64e2a

File tree

6 files changed

+144
-6
lines changed

6 files changed

+144
-6
lines changed

Server-side Development with NodeJS, Express and MongoDB/conFusionServer/authenticate.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var User = require('./models/user');
55
var JwtStrategy = require('passport-jwt').Strategy;
66
var ExtractJwt = require('passport-jwt').ExtractJwt;
77
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
8-
8+
var FacebookTokenStrategy = require('passport-facebook-token');
99
var config = require('./config.js');
1010

1111

@@ -50,4 +50,36 @@ exports.jwtPassport = passport.use(new JwtStrategy(opts,
5050
return next(err);
5151
}
5252
}
53-
exports.verifyUser = passport.authenticate('jwt', {session: false});
53+
exports.verifyUser = passport.authenticate('jwt', {session: false});
54+
55+
exports.facebookPassport = passport.use(new FacebookTokenStrategy({
56+
clientID: config.facebook.clientId,
57+
clientSecret: config.facebook.clientSecret
58+
}, (accessToken, refreshToken, profile, done) => {
59+
User.findOne({facebookId: profile.id}, (err, user) => {
60+
if (err) {
61+
return done(err, false);
62+
}
63+
if (!err && user !== null) {
64+
return done(null, user);
65+
}
66+
else {
67+
user = new User({ username: profile.displayName });
68+
user.facebookId = profile.id;
69+
user.firstname = profile.name.givenName;
70+
user.lastname = profile.name.familyName;
71+
user.save((err, user) => {
72+
if (err)
73+
return done(err, false);
74+
else
75+
return done(null, user);
76+
})
77+
}
78+
});
79+
}
80+
));
81+
82+
83+
84+
85+
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module.exports = {
22
'secretKey': '12345-67890-09876-54321',
3-
'mongoUrl' : 'mongodb://localhost:27018/conFusion'
3+
'mongoUrl' : 'mongodb://localhost:27018/conFusion',
4+
'facebook': {
5+
clientId: '631701180833184',
6+
clientSecret: '0a9d5e7e41284acac930190f51996624'
7+
}
48
}

Server-side Development with NodeJS, Express and MongoDB/conFusionServer/models/user.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var User = new Schema({
1212
type: String,
1313
default: ''
1414
},
15+
facebookId: String,
1516
admin: {
1617
type: Boolean,
1718
default: false

Server-side Development with NodeJS, Express and MongoDB/conFusionServer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"morgan": "~1.9.1",
2020
"multer": "^1.3.1",
2121
"passport": "^0.4.0",
22+
"passport-facebook-token": "^3.3.0",
2223
"passport-jwt": "^4.0.0",
2324
"passport-local": "^1.0.0",
2425
"passport-local-mongoose": "^5.0.1",
Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,94 @@
11
<html>
2-
<title>conFusion</title>
2+
<title>This is index.html</title>
33
<body>
4-
<h1>conFusion :Index.html</h1>
4+
<script>
5+
// This is called with the results from from FB.getLoginStatus().
6+
function statusChangeCallback(response) {
7+
console.log('statusChangeCallback');
8+
console.log(response);
9+
// The response object is returned with a status field that lets the
10+
// app know the current login status of the person.
11+
// Full docs on the response object can be found in the documentation
12+
// for FB.getLoginStatus().
13+
if (response.status === 'connected') {
14+
// Logged into your app and Facebook.
15+
testAPI();
16+
} else {
17+
// The person is not logged into your app or we are unable to tell.
18+
document.getElementById('status').innerHTML = 'Please log ' +
19+
'into this app.';
20+
}
21+
}
22+
23+
// This function is called when someone finishes with the Login
24+
// Button. See the onlogin handler attached to it in the sample
25+
// code below.
26+
function checkLoginState() {
27+
FB.getLoginStatus(function(response) {
28+
statusChangeCallback(response);
29+
});
30+
}
31+
32+
window.fbAsyncInit = function() {
33+
FB.init({
34+
appId : '631701180833184',
35+
xfbml : true,
36+
version : 'v2.10'
37+
});
38+
FB.AppEvents.logPageView();
39+
40+
// Now that we've initialized the JavaScript SDK, we call
41+
// FB.getLoginStatus(). This function gets the state of the
42+
// person visiting this page and can return one of three states to
43+
// the callback you provide. They can be:
44+
//
45+
// 1. Logged into your app ('connected')
46+
// 2. Logged into Facebook, but not your app ('not_authorized')
47+
// 3. Not logged into Facebook and can't tell if they are logged into
48+
// your app or not.
49+
//
50+
// These three cases are handled in the callback function.
51+
52+
FB.getLoginStatus(function(response) {
53+
statusChangeCallback(response);
54+
});
55+
};
56+
57+
(function(d, s, id){
58+
var js, fjs = d.getElementsByTagName(s)[0];
59+
if (d.getElementById(id)) {return;}
60+
js = d.createElement(s); js.id = id;
61+
js.src = "//connect.facebook.net/en_US/sdk.js";
62+
fjs.parentNode.insertBefore(js, fjs);
63+
}(document, 'script', 'facebook-jssdk'));
64+
65+
66+
// Here we run a very simple test of the Graph API after login is
67+
// successful. See statusChangeCallback() for when this call is made.
68+
function testAPI() {
69+
console.log('Welcome! Fetching your information.... ');
70+
FB.api('/me', function(response) {
71+
console.log('Successful login for: ' + response.name);
72+
document.getElementById('status').innerHTML =
73+
'Thanks for logging in, ' + response.name + '!';
74+
});
75+
}
76+
</script>
77+
78+
<h1>Index.html</h1>
579
<p>This is the contents of this file</p>
6-
</body>
80+
81+
<!--
82+
Below we include the Login Button social plugin. This button uses
83+
the JavaScript SDK to present a graphical Login button that triggers
84+
the FB.login() function when clicked.
85+
-->
86+
87+
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
88+
</fb:login-button>
89+
90+
<div id="status">
91+
</div>
92+
</body>
93+
794
</html>

Server-side Development with NodeJS, Express and MongoDB/conFusionServer/routes/users.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,17 @@ usersRouter.get('/logout',cors.corsWithOptions, (req, res,next) => {
7575
}
7676
});
7777

78+
// Facebook Connect
79+
80+
usersRouter.get('/facebook/token', passport.authenticate('facebook-token'), (req, res) => {
81+
if (req.user) {
82+
var token = authenticate.getToken({_id: req.user._id});
83+
res.statusCode = 200;
84+
res.setHeader('Content-Type', 'application/json');
85+
res.json({success: true, token: token, status: 'You are successfully logged in!'});
86+
}
87+
});
88+
89+
90+
7891
module.exports = usersRouter;

0 commit comments

Comments
 (0)