jaredhanson / passport

Simple, unobtrusive authentication for Node.js.
https://www.passportjs.org?utm_source=github&utm_medium=referral&utm_campaign=passport&utm_content=about
MIT License
22.93k stars 1.24k forks source link

always unauthorized #63

Closed Freyskeyd closed 12 years ago

Freyskeyd commented 12 years ago

Yo,

I've a small problem.

All my test is unauthorized...

my code :

global.passport = require('passport');
app.configure(function(){
  app.set('port', config.app.port);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.favicon());
  app.use(express.logger({stream:logger.getLogFile()}));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({"secret": config.app.session_secret}));
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(app.router);
  app.use(require('stylus').middleware(__dirname + '/public'));
  app.use(express.static(path.join(__dirname, 'public')));
});
var LocalStrategy = require('passport-local').Strategy;

passport.use(new LocalStrategy({
    usernameField: 'form_login_inputUsername',
    passwordField: 'form_login_inputPassword'
  },function(username, password, done) {
      return done(null, {});
    }
));

app.post('/',passport.authenticate('local'), users.login);

when i post my login form i get a "unauthorized" and i've no information about what...

Can you help me? Thank's

jaredhanson commented 12 years ago

What does the form look like? Make sure the field names are set to form_login_inputUsername and form_login_inputPassword, as configured and that it is posting to /.

If that doesn't help try this trick to debug the request:

app.use(function(req, res, next) {
  console.log(req.method + ' ' + req.url + ' HTTP/' + req.httpVersion);
  Object.keys(req.headers).forEach(function(field) {
    console.log(field + ': ' + req.headers[field])
  });
  console.dir(req.body);

  next();
}

Which will help pinpoint what the form is posting.

Freyskeyd commented 12 years ago

Hey thank's for replying

I get this :

POST / HTTP/1.1 host: 127.0.0.1:81 connection: keep-alive content-length: 62 cache-control: max-age=0 origin: http://127.0.0.1:81 user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_82) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4 content-type: application/x-www-form-urlencoded accept: text/html,application/xhtml+xml,application/xml;q=0.9,/_;q=0.8 referer: http://127.0.0.1:81/ accept-encoding: gzip,deflate,sdch accept-language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4 accept-charset: UTF-8,*;q=0.5 cookie: connect.sid=s%3AMAbIlsnBgEiiZ6xPP3YU3yvR.egAZGOB2M%2Fjip7qZs40nIMySnVKPe3GLZvvEQa2xfKM undefined

don't know what to do...

:/

Thank's! Le 30 sept. 2012 à 19:07, Jared Hanson notifications@github.com a écrit :

What does the form look like? Make sure the filed names are set to form_login_inputUsername and form_login_inputPassword, as configured and that it is posting to /.

If that doesn't help try this trick to debug the request:

app.use(function(req, res, next) { console.log(req.method + ' ' + req.url + ' HTTP/' + req.httpVersion); Object.keys(req.headers).forEach(function(field) { console.log(field + ': ' + req.headers[field]) }); console.dir(req.body);

next(); } Which will help pinpoint what the form is posting.

— Reply to this email directly or view it on GitHub.

Freyskeyd commented 12 years ago

ok i found the problem. i add use() before the express.bodyParser()

surveyconnectionHH commented 10 years ago

Hi Freyskeyd, i seem to have a similar problem. What exactly do you mean with you added use() before the express.bodyParser()?

Freyskeyd commented 10 years ago

wow, i don't remember...

Maybe the password.use before body parser?

teddy-mczieuwa commented 6 years ago

I am also having this problem and i dont understand the fix @Freyskeyd. Pls what is the fix? I would really appreciate some help. Thanks

rklra commented 5 years ago

I'm also having the same issue, @Freyskeyd if you could send an updated version of your code, or try to explain the fix it would be much appreciated

Freyskeyd commented 5 years ago

wow, I don't know... it was 6 years ago... :/