Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does passportjs LocalStrategy allow more parameters than the default username and password?

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

like image 865
Alaa Awad Avatar asked May 18 '14 21:05

Alaa Awad


People also ask

What are Passportjs strategies?

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.

What is Passportjs used for?

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.

What is passport Localstrategy?

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.


2 Answers

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
  }
}));
like image 78
Gautam Avatar answered Sep 19 '22 06:09

Gautam


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)

like image 35
Spearfisher Avatar answered Sep 20 '22 06:09

Spearfisher