Passport js "Missing Credentials" Local Custom Callback

I have a very nasty problem with Node js, express 4 and passport. I have tried all the options that I can think of, but there is still no solution. Could not 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 the passport does not receive credentials and says to console.log (info) "Missing credentials", although when the username and password are registered above, they are correct. The local strategy must also be properly configured:

 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 starts because of the "Missing credentials".

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

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

the problem does not exist.

Does anyone know what I don't see here?!?!

Thanks!

+6
source share
2 answers

How do you format data in an HTML form

Here I use Postman to demonstrate your problem:

enter image description here This is not good

Just what I want Just the conclusion I want

Does not close (req, res)

Although this may be a bit unrelated, the same problem arises if I did not.

  • Call req and res parameters before starting the passport. authenticate
  • After closing passport.authenticate terminate it with (req, res)

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

 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 a 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 }) 
+1
source

One minor mistake or correction is required.

You do:

 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 and 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); 

That should work.

-1
source

Source: https://habr.com/ru/post/986886/


All Articles