Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
meepeek opened this issue Apr 5, 2014 · 4 comments
Closed

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

meepeek opened this issue Apr 5, 2014 · 4 comments

Comments

@meepeek
Copy link

meepeek commented Apr 5, 2014

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
Copy link

SimeonC commented Aug 22, 2014

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
Copy link

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
Copy link
Owner

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
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants