Node.js/passport-Facebook authentication

随声附和 提交于 2019-12-13 04:42:47

问题


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:

-passport@0.2.1

-passport-facebook@1.03

-express@4.11.1

**** 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.


回答1:


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.



来源:https://stackoverflow.com/questions/28015906/node-js-passport-facebook-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!