Luis Gonzalez
Luis Gonzalez

Reputation: 111

Giving bad request with Passport JS

I having a problem with the passport req.login, when the user logs in with it´s email and password and passport executes it routes to bad request instead of displaying /productos2, I am using ejs and mongoose

I already tried to request the password and the username directly on the req.login, and still doesn´t work

app.get("/productos2", function(req, res, next) {
    if(req.isAuthenticated()) {
          next();
    res.render("productos2");
    } else {
        res.redirect("/login");
    }
});


app.post("/login", function(req, res, next) {

    const user = new Credential ({
    username: req.body.email,
    password: req.body.password
    });


    req.login(user, function(err) {
        if (err) {
            console.log(err);
        } else {
        passport.authenticate("local")(req, res, function(){
        res.redirect("/productos2");
      });
     }
   });
 });

So hat I´m trying to do is to be route it succesfully to productos2 route.

Upvotes: 1

Views: 79

Answers (1)

Alwin Jose
Alwin Jose

Reputation: 773

Try this.

  passport.authenticate("local")(req, res, function(){
    res.redirect("/productos2");
  });

to

  passport.authenticate('local', {
    successRedirect : '/productos2',
    failureRedirect : '/login',
    failureFlash : true // allow flash messages
  });

Upvotes: 1

Related Questions