NodeJS - Passport local strategy gives me 404

ELopez Source

I have a tremendous headche with a problem when I try to login using Passport. I'm making a post request to /login with an email and password. Passport authenticates it correctly, err isn't called and then return res.redirect('/user') gets called too but it returns 404 and doesn't redirect. I don't know why this is happening. Here's my code:

Server (index.js):

const
  express = require('express'),
  app = express(),
  mongoose = require('mongoose'),
  passport = require('passport'),
  cookieSession = require('cookie-session'),
  bodyParser = require('body-parser'),
  keys = require('./config/keys'),
  user = require('./models/User'),
  passportService = require('./services/passport'),
  authRoutes = require('./routes/auth');

mongoose.connect(keys.mongoURI);

app.use(bodyParser.json());
app.use(
  cookieSession({
    maxAge: 15 * 24 * 60 * 60 * 1000,
    keys: [keys.cookieKey]
  })
);
app.use(passport.initialize());
app.use(passport.session());

passportService(passport);

authRoutes(app);

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
});

passportService (passport.js):

const LocalStrategy = require('passport-local').Strategy;
const mongoose = require('mongoose');
const User = mongoose.model('users');

module.exports = (passport) => {
  passport.serializeUser((user, done) => {
    done(null, user._id);
  });

  passport.deserializeUser((id, done) => {
    User.findById(id).then(user => {
      done(null, user);
    });
  });

  passport.use(
    new LocalStrategy(
    {
      usernameField: 'emailaddr',
      passwordField: 'passwd'
    },
  function(username, password, done) {

  User.findOne({ email: username }, function(err, user) {

    if(err){
      return done(err);
    }
    if(!user) {
      console.log('User not found with email: ', username);
      return done(null, false);
    }
    if(user.password != password) {
      console.log('Invalid password');
      return done(null, false)
    }

    return done(null, user);
  });

}));

}

Authentication route:

const passport = require('passport');

module.exports = app => {

  app.post('/api/login', function(req, res, next) {
  passport.authenticate('local', function(err, user, info) {
    if (err) { return next(err); }
    if (!user) { return res.redirect('/login'); }
    req.logIn(user, function(err) {
      if (err) { return next(err); }
      return res.redirect('/user');
    });
  })(req, res, next);
  });

}

There is not route for /user because I'm working with React and React Router in the client. Please I need your help!!!

javascriptnode.jsexpresspassport.js

Answers

answered 6 months ago wlh #1

I would suggest not using the custom callback on your authenticate function. Instead, check to see if after the api is called and authentication is run if you have a user attached to the request object.

// calling redirects from your api will be problematic with react router
// allow your front end to decide what route to call based on the response 

app.post('/api/login', passport.authenticate('local), function(req, res, next) {
  if(req.user) {
      res.json(req.user)

  } else {
      //handle errors here, decide what you want to send back to your front end
      //so that it knows the user wasn't found
      res.statusCode = 503;
      res.send({message: 'Not Found'})
  }
});

comments powered by Disqus