jaredhanson / passport-local

Username and password authentication strategy for Passport and Node.js.
https://www.passportjs.org/packages/passport-local/?utm_source=github&utm_medium=referral&utm_campaign=passport-local&utm_content=about
MIT License
2.74k stars 498 forks source link

Manually input variable for credentials instead of using a form #69

Closed meepeek closed 10 years ago

meepeek commented 10 years ago

From the piece of the code that was given for an example in passportjs documentation

passport.use(new LocalStrategy( function(username, password, done) {

I finally realise that PassportJS try to automatically get the username / password field in the post request automatically but the problem is that I am using angularjs and ajax to do the submission so my post request for the login would be just a json object contains information scraped from the login form. Is there any option to manually pass the username and password directly as variable ?

SimeonC commented 10 years ago

I think what you do is this:

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

As long as the json you post from angularjs is something like: {username: 'xxxxxxx', password: 'xxxxxxx'} that should work according to https://github.com/jaredhanson/passport-local/blob/master/lib/strategy.js#L71

shiftyRZA commented 10 years ago

I have the same issue (also using Angular.js). It is impossible to use the username and password from json if it is nested, say:

POST /api/users/login
{
  user: {
    email: '936@xkcd.com',
    password: 'correct horse battery staple' 
  }
}

I've tried setting usernameField to user.email and passwordField to user.password, but that didn't work. I'll try messing with the lib/utils.js lookup function.

Alternatively, maybe allow us to override the object passed to Strategy.authenticate? In my case, it would be something like:

passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password',
    object: req.body.user
  },
  function(username, password, done) {
    // ...
  }
));
jaredhanson commented 10 years ago

It takes a form-style notation for nesting:

passport.use(new LocalStrategy({
    usernameField: 'user[email]',
    passwordField: 'user[password]',
    object: req.body.user
  },
  function(username, password, done) {
    // ...
  }
));
shiftyRZA commented 10 years ago

Thanks!