We have answer of your question!

100% solved queries, no empty question

Question: NodeJS Passport function not getting called


0

Advertisement


I am trying to make a Facebook login

var passport = require('passport');
var express = require('express');
var FacebookStrategy = require('passport-facebook').Strategy;
var bodyParser = require('body-parser');
var app = express();

passport.use(new FacebookStrategy({
        clientID: '-',
        clientSecret: '-',
        callbackURL: "http://localhost:3000"
    },
    function(accessToken, refreshToken, profile, done) {
        console.log("Token:", accessToken);
        console.log("Refresh token:", refreshToken);
        done(null);
    }
));

app.get('/auth/facebook',
    bodyParser.urlencoded({ extended: true }),
    passport.authenticate('facebook'));

app.get('/', function (req, res) {
    res.send("Helo");
});

app.listen(3000);

But why I never get the console logs from the function inside the FacebookStrategy ? The login works perfectly and I get redirected to the callbackURL but that function looks like it doesn't even get called

Question author User2391356 | Source

Answer


1


Advertisement


You need to initialize passport before using its authentication strategies.

From Passport Documantation itself:

In a Connect or Express-based application, passport.initialize() middleware is required to initialize Passport. If your application uses persistent login sessions, passport.session() middleware must also be used.

Note that enabling session support is entirely optional, though it is recommended for most applications. If enabled, be sure to use express.session() before passport.session() to ensure that the login session is restored in the correct order.

add these lines before passport.use(new FacebookStrategy({...})

app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: 'your_express_session_secret' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);

Go through Passport configure Documentation to understand it better.

Dont forget to import required node modules:

var cookieParser = require('cookie-parser');
var expressSession = require('express-session');

Hope this helps!

Answer author Ravi-shankar

Advertisement


Tickanswer.com is providing the only single recommended solution of the question NodeJS Passport function not getting called under the categories i.e node.js , passport.js , . Our team of experts filter the best solution for you.

Related Search Queries:

passport local strategy not called , passport.authenticate is not a function ,

You may also add your answer!