jaredhanson / passport-local

Username and password authentication strategy for Passport and Node.js.
https://www.passportjs.org/packages/passport-local/?utm_source=github&utm_medium=referral&utm_campaign=passport-local&utm_content=about
MIT License
2.73k stars 498 forks source link

Callback from Passportjs doesn't send data back to client but renders it in the requested url #157

Closed govindrai closed 7 years ago

govindrai commented 7 years ago

I am using Passport.js for authentication, React for frontend, and Express for backend.

When my user logs in, they are successfully authenticated and the user gets logged in, but unfortunately the user info which is supposed to be sent back to my client is completed as a post request and gets rendered to the screen.

This is strange because all my other ajax requests through axios, I always get a response back to the client side, but for for this particular route it does not. Maybe it is a passport limitation? Regardless:

Here is my route definition in Express:

router.post("/admin", (req, res, next) => {
  passport.authenticate("local", function(err, user, info) {
    if (err) {
      return next(err);
    }
    if (!user) {
      return res.json({});
    }
    req.logIn(user, function(err) {
      if (err) {
        return next(err);
      }
      return res.json(user);
    });
  })(req, res, next);
});

Here is the ajax call using axios (no response is ever logged because the page ends up redirecting somehow)

axios
  .post("/auth/admin", { email, password })
  .then(res => console.log("RESPONSE", res));

How can I make it so the response is sent to my client and not rendered? Feel free to answer on SO