7

I have a really annoying problem with Node js, express 4 and passport. I have tried all options I can think of, but still no solution. Couldn't find a solution from other posts for this problem.

Code:

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

    console.log(req.body.user_email);
    console.log(req.body.user_password);

    passport.authenticate('local', function(err, user, info) {

        console.log(info);
...

The problem is that for some reason, passport does not get the credentials and says at the console.log(info) "Missing credentials", although when the username and password are logged above they are correct. The local strategy should also be configured properly:

passport.use(new LocalStrategy ({

    usernameField: 'user_email',
    passwordField: 'user_password',
    },

    function(username, password, done) {

        console.log(username);
        console.log(password);
...

The "function(username, password, done)" never gets run, because of the "Missing credentials".

The funny part is that from another html/ejs page where a call for authentication is made with:

app.post('/login_user', passport.authenticate('local', { successRedirect: '/loginSuccess', failureRedirect: '/loginFailure' }));

the problem does not exist.

Does anybody know what I am missing here?!?!

Thanks!

Murre
  • 141
  • 2
  • 9

4 Answers4

1

How you format the data in HTML form

Here I'm using Postman to demonstrate your issue:

enter image description here This is not good

Just the output that I want Just the output that I want

Not closing with (req, res)

Although this might not be related a bit, but the same problem occurs if I didn't do this.

  1. Invoke req and res parameters before initiating the passport.authenticate
  2. After you close the passport.authenticate, finish it with (req, res)

You need (req, res) at the end of your function, as so:

router.post('/signup', 
(req, res, next) => passport.authenticate('whatever-your-strategy-name-is', function(err, user, info) {

    // Just to see the sample expected output
    res.send(JSON.stringify(info)).status(200)

  })(req, res)
);

In standard familiar JS, it looks like this.

router.post('/signup', function (req, res, next) {

  passport.authenticate('whatever-your-strategy-name-is', function(err, user, info) {

    // Just to see the sample expected output
    res.send(JSON.stringify(info)).status(200)

  })(req, res) // passport.authenticate ends here
})
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Mohd.Zafranudin
  • 364
  • 4
  • 12
1

Just in case this might be useful to someone in the future.. If your using POSTMAN and your sending a POST request(of say an email and password field for example) and you find yourself seeing this "missing credentials" response make sure that the format of the response is set to JSON and not like html or text cause a simple mistake like that can get you stuck for hours

  • Ahhh, this was it! This needs more upvotes. Switched my postman to use raw json and it worked! – Kitson Feb 09 '22 at 14:32
1

I got this error of missing credentials is when i was sending email as a first parameter, but passport local strategy accepts first parameter a Username So you need to set it as email like that :

passport.use(
    new LocalStrategy({ usernameField: "email" }, (email, password, done) => {
-2

There is one minor mistake or correction required.

You are doing:

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

    console.log(req.body.user_email);
    console.log(req.body.user_password);

    passport.authenticate('local', function(err, user, info) {

        console.log(info);

Just create a user variable and set the passed in user_email & user_password as shown below.

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

    console.log(req.body.user_email);
    console.log(req.body.user_password);

    var user = req.body;

    passport.authenticate('local', function(err, user, info) {

        console.log(info); 

This should work.

AAhad
  • 2,805
  • 1
  • 25
  • 42