1
votes

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!!!

1
Please, instead of bagging for help, ask a simple question to make it easier to understand your concerne.ibenjelloun
@ibenjelloun I'm new here. I have edited the post, I think is easier to understand. Greetings.ELopez
Where is your React / React Router code? Sounds like Passport is working, but maybe you're not adding /user to your front-end routes...user
@user my React / React Router code is working. If I manipulate the url manually to /user the view is displayed.ELopez
@ELopez Do you need to add a Passport route for /user that points to the .html file containing the React app? If the JS doesn't get served to the browser, the client-side routing won't take effect.user

1 Answers

1
votes

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'})
  }
});