Toby
Toby

Reputation: 719

Express post /signup 500 unknown error with passport

I have a nodeJS application using express and passport. Im trying to handle the post requests to passport for a login. Im getting an unknown error in my server console: POST /signup 500

Second question, any idea's why its not working from a quick glance of what I have below. I know its not all the code, but maybe there is something obvious..?

app.js:

..
var passport = require('passport');
require('./config/passport')(passport);
..
var routes = require('./routes/index')(app, passport);
..

Passport.js defines my handling of users and the database. It defines a localStragery called local-signup.

/routes/index.js:

module.exports = function(app, passport) {
..
var express = require('express');
var router = express.Router();
..

router.post('/signup',
    passport.authenticate('local-signup', {
      successRedirect : '/profile',
      failureRedirect : '/signup' })
    );
..
return router
}

Update:

My code is actually working, it is putting new users in the database, its just the application is returning 500. Using loads of console.log() and trial and error I have tied it down to what i returning from my passport stratagy. I have the following code wich is being hit:

..

 } else {

                console.log("-] Email is not found, creating new user");

                user = {};
                user['email']    = email;
                user['password'] = bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);

                console.log("-] Inserting into db");
                userCollection.insert( user, function(err, user) {
                  if (err) {
                    console.log("Error: " + err );
                    console.log("records: " + user );
                    throw err;
                  }

                  console.log("returned user: " + user)
                  return done(null, user);

its something to do with me returning user. If I get rid of this line and just return instead, it works fine but the call to authenticate thinks it has failed and follow that's redirect rather than the one for success. The issue is that i got this code from an application that makes use of mongoose which wraps the database stuff in an object. I don't want to do that, i want to make the db calls directly myself like the code above.

Any idea's?

Upvotes: 2

Views: 2088

Answers (2)

Daan van Hulst
Daan van Hulst

Reputation: 1436

For me the solution was to not use:

passport.authenticate('local-signup', {

But:

passport.authenticate('local', {

This is the default if you do not set a name when registering your strategy like so:

passport.use(new LocalStrategy({

You can pass a name in if you have multiple LocalStrategies:

passport.use('local-signup', new LocalStrategy({

Upvotes: 0

Ben
Ben

Reputation: 7597

You're not making use of the error handling in the API.

Try something like this instead:

passport.authenticate('local-signup', {
  successRedirect: '/profile',
  failureRedirect: '/login',
  failureFlash: true })

... this tells passport to 'flash' any error resulting from the callback (assuming there's one defined as part of the verify stage of local-signup).

Now, assuming local-signup in your application is set up like so:

var local-signup = require('passport-local').Strategy;

... you should be able to sort the error handling pretty easily. Here's a sample from the configuration docs at http://passportjs.org/guide/configure/ -- note how errors are returned:

passport.use(new LocalStrategy(
  function(username, password, done) {
    User.findOne({ username: username }, function (err, user) {
      if (err) { return done(err); }
      if (!user) {
        return done(null, false, { message: 'Incorrect username.' });
      }
      if (!user.validPassword(password)) {
        return done(null, false, { message: 'Incorrect password.' });
      }
      return done(null, user);
    });
  }
));

Upvotes: 1

Related Questions