Note that there are some explanatory texts on larger screens.

plurals
  1. PONodejs Passport authenticate callback not being called
    primarykey
    data
    text
    <p>Using Nodejs Passport, I was testing out what happens when an error condition occurs using the following code:</p> <pre><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); }); </code></pre> <p>Using the route /logintest?username=bob&amp;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.</p> <p>I also tried <code>return done({'message':'test'});</code> and "[object Object]" was displayed in the console and in the browser.</p> <p>Is this not working properly or am I missing something?</p> <p>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:</p> <pre><code>... })(req, res, next); }, function(err, req, res, next) { // failure in login test route return res.send({'status':'err','message':err.message}); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload