Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:51:14+00:00 2026-06-05T16:51:14+00:00

I’m trying to integrate passport into my nodejs server using connect, but can’t seem

  • 0

I’m trying to integrate passport into my nodejs server using connect, but can’t seem to do it properly. All the guides/examples use expressJS, so I tried my best to reformat the code to work with my code, but I can’t seem to get it to work. The related parts are written below. Does anybody have any advice on what might be the problem? passport.authenticate() never seems to be called (at least the console.log message within the facebook authentication callback never prints). I’m currently not saving anything to a database, so the issue should hopefully be something really simple that I’m just missing.

The only thing that comes to mind is the potential callback I have for facebook, which is a localhost url (since i’m still developing this locally). I was able to authenticate with facebook just fine using everyauth (from a purely local instance), but switched to passportJS since this I was having different issues there that passportJS seemed to address.

passport = require('passport');
  fpass = require('passport-facebook').Strategy;

passport.serializeUser(function(user,done){
    done(null, user);
});
passport.deserializeUser(function(obj,done){
    done(null,obj);
});

passport.use(new fpass({
        clientID:'facebook app id',
        clientSecret:'facebook app secret',
        callbackURL:'http://localhost:3000/auth/facebook/callback'
    },
    function(accessToken, refreshToken, fbUserData, done){
        console.log('got here');
        return done(null,fbUserData);
    }
));


    
    function checkLoggedIn(req, res, next){
        console.log("req.user: " + req.user);
        if(req.user)
            next();
        else{
            console.log('\nNot LOGGED IN\n');
            if(req.socket.remoteAddress || req.socket.socket.remoteAddress == '127.0.0.1'){
                var folder,contentType;
                console.log('req url = '+req.url);
                if(req.url == '/'){
                    folder = __dirname + '/landingPage.html';
                    contentType = 'text/html';
                }
                else if(req.url == '/auth/facebook'){
                    passport.authenticate('facebook');
                    return;
                }
                else if(req.url == '/auth/facebook/callback'){
                    passport.authenticate('facebook', {failureRedirect: '/failbook', successRedirect:'/'});
                    return;
                }
                if(folder){
                    console.log('got to folder part\n\n');
                    fs.readFile(folder, function(error, content){
                      if(error){
                        res.writeHead(500);
                        res.end();
                      }
                      else{
                        res.writeHead(200, {'Content-Type': contentType});
                        res.end(content);
                      }
                    });
                  }
                    else{ res.writeHead(500); res.end();}
            }
            else {res.writeHead(500); res.end();}
        }
    }
    
  connect.createServer(
    connect.cookieParser(),
    connect.bodyParser(),
    connect.session({secret:'wakajakamadaka'}),
    passport.initialize(),
    passport.session(),
    checkLoggedIn).listen(8888);
  console.log('Server has started.');
}

Does anybody have any advice or see a glitch in what I’m doing? My other two alternatives are switching back to everyauth and figuring out what’s going on there, or switching to ExpressJS, but I’d rather not go with either of those options.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:51:16+00:00Added an answer on June 5, 2026 at 4:51 pm

    passport.authenticate returns a function which is typically used in the middleware chain, which gets invoked with req and res by Express.

    It will work standalone inside other middleware or a route handler, such as your checkLoggedIn function, but you have to explicitly invoke the function returned with req, res, and next to let it process the request.

    else if(req.url == '/auth/facebook'){
      // NOTE: call the function returned to process the request
      passport.authenticate('facebook')(req, res, next);
      return;
    }
    

    Looks good otherwise. Let me know if that gets you up and running.

    UPDATE

    Jared has helped me a little bit outside of stackoverflow, and we have figured out the problem fully. I’m updating this answer to include the new information we found.

    1) One issue is clearly that the redirect() middleware exists in Express but not in Connect (at least not the current version). When using connect, you’ll need to change line 97 of authenticate.js in the PassportJS module from return res.redirect(options.successRedirect); to:

    var redirect = function(redirectionURL){
        res.writeHead(302, {'location':redirectionURL});
        res.end();             
    }
    
    return redirect(options.successRedirect);
    

    note – the options.successRedirect above is the same as the successRedirect in the callback function code. This would be the line passport.authenticate('facebook', {failureRedirect: '/failbook', successRedirect:'/'}); in the code in my question.

    2) The other is that I’m using the cluster module in my app (not something that shows in my code snippet above). This means that session data isn’t being read properly by the nodeJS app, which resulted in my problems. This problem will affect any library that relies on sessions, including all other authentication modules that use sessions (e.g. everyauth, connect-auth, etc.) I will quote Jared for a solution:

    Quick reply: are you using cluster or spinning up multiple server
    instances?

    If so, the built-in MemoryStore for the session won’t work properly,
    and you need to switch to something like this:
    https://github.com/visionmedia/connect-redis

    Or one of the other session stores listed here:
    https://github.com/senchalabs/connect/wiki

    The reason is that you’ll end up hitting a server which didn’t serve
    the original request, and thus doesn’t have that session information
    in its own memory. That sounds like what could be going on if
    deserializeUser isn’t called 100% of the time.

    Hope this helps whoever makes it here!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.