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 8466091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:14:25+00:00 2026-06-10T15:14:25+00:00

Using Nodejs Passport, I was testing out what happens when an error condition occurs

  • 0

Using Nodejs Passport, I was testing out what happens when an error condition occurs using the following code:

passport.use(new LocalStrategy(
  function(username, password, done) {
    // asynchronous verification, for effect...
    process.nextTick(function () {
      findByUsername(username, function(err, user) {
    console.log('in auth function');
      return done('errortest');
        if (err) { return done(err); }
        if (!user) {
          return done(null, false, { message: 'Unknown user ' + username });
        }
        if (user.password != password) {
          return done(null, false, { message: 'Invalid password' });
        }
        return done(null, user);
      })
    });
  }
));

app.get('/logintest', function(req, res, next) {
console.log('before authenticate');
  passport.authenticate('local', function(err, user, info) {
console.log('authenticate callback');
    if (err) { return res.send({'status':'err','message':err.message}); }
    if (!user) { return res.send({'status':'fail','message':info.message}); }
    req.logIn(user, function(err) {
      if (err) { return res.send({'status':'err','message':err.message}); }
      return res.send({'status':'ok'});
    });
  })(req, res, next);
});

Using the route /logintest?username=bob&password=s I expected to see in the console, “before authenticate” then “in auth function” then “authenticate callback” but it only shows the first two followed by “errortest”, and “errortest” is displayed in the browser.

I also tried return done({'message':'test'}); and “[object Object]” was displayed in the console and in the browser.

Is this not working properly or am I missing something?

EDIT: As per Jared Hanson’s response, adding this error handler function as the third argument to app.get() allows me to catch the error and return the appropriate json:

...
    })(req, res, next);
  },
  function(err, req, res, next) {
    // failure in login test route
    return res.send({'status':'err','message':err.message});
  });
  • 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-10T15:14:27+00:00Added an answer on June 10, 2026 at 3:14 pm

    You’re understanding it perfectly, and its working as intended.

    If any error occurs, Passport immediately next()‘s with that error. You can use error handling middleware (details: http://expressjs.com/guide/error-handling.html) if you want to handle that error in a custom manner.

    Custom callbacks are primarily used to deal with authentication success or failure (user == false). Errors, like DB connectivity, etc, are not passed back to the callback, in favor of error handling middleware described above. I’ve considered changing this, but haven’t found a compelling reason to. But, if you’ve got a use case that isn’t covered by the above, let me know.

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

Sidebar

Related Questions

I'm trying to integrate passport into my nodejs server using connect, but can't seem
I'm using Nodejs and Socket.io. When the client connects, new JavaScript objects are created.
I am using nodejs. And calling something like the following from a javascript file:
Using the nodejs v.6.7 with faye v.7.1 I am getting error /path/livestats/node_modules/faye0.7/faye.js:18 ID_LENGTH: <%=
In nodeJS I am using the passport module for authentication. I would like to
I am currently using nodejs and I have a function which return a body
I'm using nodejs 0.4.7 to make the request, this is my code: var post_data
Just having some issues with building a function using nodeJS. It doesn't seem to
I'm trying to implement long polling technique using NodeJS. I have this basic code
I use passport.js to handle auth on my nodejs + express.js application. I setup

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.