Passport-local: Manually input variable for credentials instead of using a form

Created on 5 Apr 2014  路  4Comments  路  Source: jaredhanson/passport-local

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 ?

Most helpful comment

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) {
    // ...
  }
));

All 4 comments

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

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: '[email protected]',
    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) {
    // ...
  }
));

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) {
    // ...
  }
));

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

coder90 picture coder90  路  5Comments

jacargentina picture jacargentina  路  7Comments

JonathanSum picture JonathanSum  路  11Comments

EvHaus picture EvHaus  路  25Comments

ghost picture ghost  路  3Comments