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
Strategies are responsible for authenticating requests, which they accomplish by implementing an authentication mechanism. Authentication mechanisms define how to encode a credential, such as a password or an assertion from an identity provider (IdP), in a request.
Passport is a popular, modular authentication middleware for Node. js applications. With it, authentication can be easily integrated into any Node- and Express-based app. The Passport library provides more than 500 authentication mechanisms, including OAuth, JWT, and simple username and password based authentication.
passport-local is the strategy you would use if you are authenticating against a username and password stored 'locally' i.e. in the database of your app - 'local' means local to your application server, not local to the end user. passport-jwt is the strategy for using JSON Web Tokens.
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
}
}));
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With