26

Been working on this for a few hours now, pretty frustrating...

router.post('/', passport.authenticate('local-signup', function(err, user, info) {
  console.log(err);
}), function(req, res){
  console.log(req);
  res.setHeader('Content-Type', 'application/json');
  res.send(JSON.stringify({ a: 1 }));
});

When I run this, i used console.log, output was { message: 'Missing credentials' }, which leads me to believe that the body parser is not properly parsing the body message. When I use this route however...

router.post('/', function(req, res){
  console.log(req.body);
  res.setHeader('Content-Type', 'application/json');
  res.send(JSON.stringify({ a: 1 }));
});

When I used console.log, the output was {password: 'password', email: 'email@email.com'}, which indicates that the req.body variables are being set properly and are available.

app.js

var express = require('express');
var app = express();
var routes = require("./config/routes.config");
var models = require("./config/models.config");
var session = require('express-session');
var bodyParser = require('body-parser');

models.forEach(function(model){
  GLOBAL[model] = require('./models/'+model+".model");
});
var passport = require("./config/passport.config");

app.use( bodyParser.urlencoded({ extended: true }) );
app.use(session({ secret: 'simpleExpressMVC', resave: true, saveUninitialized: true  }));
app.use(passport.initialize());
app.use(passport.session());
Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52
l2silver
  • 3,283
  • 6
  • 23
  • 28

9 Answers9

61

I see your req.body contains {password: 'password', email: 'email@email.com'}. email is not what passportjs is looking for, but username is. You can either change your input names on your HTML/JS, or you can change the default parameters passportjs is looking for in req.body. You need to apply these changes where you define your strategy.

passport.use(new LocalStrategy({ // or whatever you want to use
    usernameField: 'email',    // define the parameter in req.body that passport can use as username and password
    passwordField: 'password'
  },
  function(username, password, done) { // depending on your strategy, you might not need this function ...
    // ...
  }
));
Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52
8

In Router.post:

router.post('/', passport.authenticate('local-signup', {
    successRedirect: '/dashboad',
    failureRedirect: '/',
    badRequestMessage: 'Your message you want to change.', //missing credentials
    failureFlash: true
}, function(req, res, next) {
...
Dũng IT
  • 2,751
  • 30
  • 29
7

I know there are lots of answers here, but this is as clear as it gets.

From the official passport docs

Passportjs documentation

"By default, LocalStrategy expects to find credentials in parameters named username and password. If your site prefers to name these fields differently, options are available to change the defaults."

So if you find yourself with this issue you have two solutions (most probably).

  1. Rename your body parameters in your front-end as username and password

  2. Define to your passport instance how you're going to name them with the following code:

    passport.use(new LocalStrategy({
        usernameField: 'email', //can be 'email' or 'whateveryouwant'
        passwordField: 'passwd' //same thing here
      },
      function(username, password, done) {
        // ...
      }
    ));
    
Lheonair
  • 468
  • 5
  • 14
  • 1
    Thank you, you're answer further clarified and expanded upon the accepted answer, which helped wrapping my mind around the same error i was getting. Much appreciated. – xv8 Aug 09 '21 at 22:59
  • Glad it helped someone. Cheers – Lheonair Aug 11 '21 at 08:15
1

There could be a mistake in field-type that's why passport.js is not able to recognize the correct field and gives error .

CORRECTION: check in .ejs file if your username and password fields correctly state type. type="password" id="password" name="password"//CORRECTLY SPECIFIED.

What you might have done is : Type:"text" in ejs file and told passport to look for type:"password"

Tip : Check for spelling mistakes in type fields. eg: type:password in ejs & type:Password

  • 2
    Hello and welcome to SO! Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) For example there is no point in writing Silly in the begging. It might be considered offensive. Try to be as in topic as you can in your answers. – Tomer Shetah Dec 29 '20 at 11:12
  • Understood! Appreciate your help . – Anshul Tyagi Dec 29 '20 at 13:37
1

In my own case, the cause of this was my body-parser or express configuration, whereby inputs from the form were not received by my application. So I did this:

// configuring express
app.use(express.static(path.join(__dirname, "public")));
// body-parser 
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
0

In my case I just miss spelled usernameField as usernameFiled.

Make sure you spell correctly usernameField and passwordField.

Hasan Sefa Ozalp
  • 6,353
  • 5
  • 34
  • 45
0

Mine was spelling mistake issue. I hade usernameFeild: 'email',

However, it should be usernameField: 'email',

0

if you are using passport local mongoose as a plugin with your user model.

Try this:

passport.use(new LocalStrategy({
  usernameField: 'email'
}, User.authenticate()));
olawalejuwonm
  • 1,315
  • 11
  • 17
0

In my case add all the field inside of config works:

passport.use(new LocalStrategy({
  usernameField : 'email',
  passwordField : 'password',
  passReqToCallback : true
}))
david pincheira
  • 171
  • 1
  • 6