Reputation: 2496
I am trying to implement a simple Facebook login integration using passport (passport-facebook) authentication middleware. Regarding to this documentation over here I have some code here from my server.js:
var express = require('express'),
passport = require('passport'),
FacebookStrategy = require('passport-facebook').Strategy,
app = express();
// Facebook authentication:
passport.use(
new FacebookStrategy({
clientID: "fb client id here",
clientSecret: "fb client secret key here",
callbackURL: "http://localhost/web-apps/facebook-passport/auth/facebook/"
},
function(accessToken, refreshToken, profile, done) {
console.log("auth is done!");
done(null, profile);
})
);
app.get('/web-apps/facebook-passport/auth/facebook', passport.authenticate('facebook'));
app.get(
'/web-apps/facebook-passport/auth/facebook/',
passport.authenticate('facebook', {
successRedirect: '/',
failureRedirect: '/login'
})
);
app.get('/web-apps/facebook-passport/', function(req, res) {
res.send('<a href="/web-apps/facebook-passport/auth/facebook">Login with Facebook</a>');
});
app.listen(80);
The code basically provides a link that should lead to Facebook authentication (/facebook/auth), ask the client for permission and then redirect user back to my page with some access tokens and client's details. However, when I run my server and try to do that by pressing the link I got
passport.initialize() middleware not in use
Passport stacktrace:
Error: passport.initialize() middleware not in use
at IncomingMessage.req.login.req.logIn (C:\xampp\htdocs\web-apps\facebook-passport\node_modules\passport\lib\http\request.js:44:34)
at Strategy.strategy.success (C:\xampp\htdocs\web-apps\facebook-passport\node_modules\passport\lib\middleware\authenticate.js:228:13)
at verified (C:\xampp\htdocs\web-apps\facebook-passport\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:179:18)
at Strategy.app.get.passport.authenticate.successRedirect [as _verify] (C:\xampp\htdocs\web-apps\facebook-passport\server.js:22:3)
at C:\xampp\htdocs\web-apps\facebook-passport\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:195:22
at C:\xampp\htdocs\web-apps\facebook-passport\node_modules\passport-facebook\lib\strategy.js:183:5
at passBackControl (C:\xampp\htdocs\web-apps\facebook-passport\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:126:9)
at IncomingMessage.<anonymous> (C:\xampp\htdocs\web-apps\facebook-passport\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:143:7)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:944:16
I guess I would need to add something like app.configuration
to my server-side script but these are gone in express 4.x version. I am wondering how I could fix this?
My node packages are:
**** U P D A T E ****
I've added the following code to the top of my script which was result loosing the previous error message:
app.use(bodyParser());
// Use the passport package in our application
app.use(passport.initialize());
// Create our Express router
var router = express.Router();
However, I got this new error saying
This authorization code has been used.
This is the stack trace:
FacebookTokenError: This authorization code has been used.
at Strategy.parseErrorResponse (C:\xampp\htdocs\web-apps\facebook-passport\node_modules\passport-facebook\lib\strategy.js:198:12)
at Strategy.OAuth2Strategy._createOAuthError (C:\xampp\htdocs\web-apps\facebook-passport\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:345:16)
at C:\xampp\htdocs\web-apps\facebook-passport\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:171:43
at C:\xampp\htdocs\web-apps\facebook-passport\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:177:18
at passBackControl (C:\xampp\htdocs\web-apps\facebook-passport\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:124:9)
at IncomingMessage.<anonymous> (C:\xampp\htdocs\web-apps\facebook-passport\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:143:7)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:944:16
at process._tickCallback (node.js:442:13)
Solved:
Problems with URIs.
Upvotes: 0
Views: 1507
Reputation: 3866
Try using this way in your
app.get('/web-apps/facebook-passport/auth/facebook', passport.authenticate('facebook-signup', { session: false }));
instead of
app.get('/web-apps/facebook-passport/auth/facebook', passport.authenticate('facebook'));
I think the problem is that facebook logged you an returned a token, if you dont close the session, facebook is waiting for the same token. Using {session: false} cause facebook close session after the authentication is done. You will be receiving a new token each time you authenticate.
I have had some problems like this before, i m not an expert, but also i solved it in this way.
Upvotes: 1