K
K
Konstantin Bozhkov2017-02-06 21:50:31
Node.js
Konstantin Bozhkov, 2017-02-06 21:50:31

How do username and password values ​​get into Password js?

I've lost count of how many times I cursed the day I switched from PHP to Node.js. The essence of the question is that I get email, password values ​​with Ajax.

app.post('/login', (req, res) => {
    var data = JSON.parse(req.body.remodal);
    console.log(data.email, data.password);	
  })
//passport.js
passport.use(new LocalStrategy(function(username, password, callback){
//Как передаются username и password
}))
But I have no idea how to pass them to passport.js. Please answer the question in more detail. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Damir Makhmutov, 2017-02-06
@ybiks

By default, LocalStrategy looks for the username and password fields in `req.body`. But since your data comes in a different format, you either need to manually report the necessary data to req.body, or connect the body-parser so that it decodes the incoming json, + you need to tell LocalStrategy where to get the data from. It's done like this:

const bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

passport.use(new LocalStrategy({
  usernameField: 'remodal[email]',
  passwordField: 'remodal[password]'
}, function(username, password, callback) {
  console.log(username, password);
}));

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

I have not tested the code, but I hope the idea is clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question