问题
I'm new to javascript, node,and passportjs. Sorry if this is incorrect. I'd like to use 3 parameters in my passport local strategy: username, email, password. Is it possible? If so how?
According to passportjs: "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." But can i add more paramaters?
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 logs email as password and password as some function
回答1:
There is there is the passReqToCallback argument.
passport.use(new LocalStrategy(
{
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true
},
function (req, username, password, done) {
{
fieldName: req.body.fieldName,
password: password,
username: username
}
}));
回答2:
I suspect what you are trying to do is save additional information on your user when he signs up.
All your information is store 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's 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 as it only needs 2 arguements (the email/username + the password)
回答3:
LocalStrategy only expects two parameters named username and password. But you can name them differently setting the defaults as following:
passport.use(new LocalStrategy({
usernameField: 'your_form_username_or_email_fieldname',
passwordField: 'your_form_password_fieldname'
},
function(username, password, done) {
// ...
}
));
The doc is here: http://passportjs.org/guide/username-password/
ps. The "some function" you are talking about must be the callback function done ;)
回答4:
The req.body carries the form's data (fields with names), and this is pasted into passport Local-login.
My Login Form has three field companyId, email and 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's 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 it helps someone.
回答5:
Just a note in case anyone else finds this while Googling: if req.body doesn't work for you, use req.query instead.
来源:https://stackoverflow.com/questions/23726921/does-passportjs-localstrategy-allow-more-parameters-than-the-default-username-an