Does passjs LocalStrategy have more options than the default username and password?

I am new to javascript, node and passjs. Sorry if this is not true. I would like to use 3 parameters in my local passport strategy: username, email address, password . Is it possible? If so, how?

According to the passport: "By default, LocalStrategy expects to find credentials in the parameters with a username and password. If your site prefers to name these fields in different ways, options are available to change the default values." But can I add more options?

I tried this:

passport.use('local-signup', new LocalStrategy({ // by default, local strategy uses username and password, we will override with email usernameField : 'username', emailField : 'email', passwordField : 'password', passReqToCallback : true // allows us to pass back the entire request to the callback }, function(req, username, email, password, done) { console.log("username:"+username + "email:"+email + "pwd:"+password); })); 

but it registers email as password and password as some functions

+6
source share
5 answers

There is an argument passReqToCallback.

 passport.use(new LocalStrategy( { usernameField: 'username', passwordField: 'password', passReqToCallback: true }, function (req, username, password, done) { { fieldName: req.body.fieldName, password: password, username: username } })); 
+8
source

I suspect that you are trying to save additional information about your user when he subscribes.

All your information is stored in req.body , so you just need to save it in your callback:

 passport.use(new LocalStrategy({ usernameField: 'your_form_username_or_email_fieldname', passwordField: 'your_form_password_fieldname' }, function(username, password, done) { // ... // set the user credentials newUser.username = req.body.username; newUser.email = req.body.email; newUser.password = newUser.generateHash(password); // .... } )); 

There is no need to change the local strategy, since it needs only 2 arguments (email address / username + password)

+6
source

LocalStrategy expects only two parameters with a username and password. But you can name them differently by setting default values ​​as follows:

 passport.use(new LocalStrategy({ usernameField: 'your_form_username_or_email_fieldname', passwordField: 'your_form_password_fieldname' }, function(username, password, done) { // ... } )); 

The document is here: http://passportjs.org/guide/username-password/

ps. "Some function" that you are talking about, a callback function must be executed;)

0
source

Req.body contains the form data (fields with names), and they are inserted into the Local-login passport.

My login form has three companyId fields, an email address and a password.

 passport.use('local-login', new LocalStrategy({ usernameField : 'email', passwordField : 'password', passReqToCallback : true }, function(req, email, password, done) { if (email){ email = email.toLowerCase(); var id = db.generateHash(email,'sha256',10); // here i get to companyId for the body to look at the Company users Table var table = "Users-" + req.body.companyId; db.getOneScan(table, "id", id, null, function(err,user){.....} // if user exist in data base check password is Valid and return user } 

I know this is a little late, but I hope this helps someone.

0
source

Just notice if someone else finds this during Googling: if req.body does not work for you, use req.query instead.

-1
source

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


All Articles