node.js - Node - Passport Auth - Authed Post Route hangs on form submission -
this weird one. im passport's 'local strategy' express app , i'm running odd issue.
essentially, have 3 routes. each have auth check in place.
app.get('/admin', authenticatedornot, adminroute.index); app.get('/admin/new', authenticatedornot, adminroute.newpost); app.post('/admin/new', authenticatedornot, adminroute.create);
the authenticatedornot method :
var authenticatedornot = function(req, res, next){ if(req.isauthenticated()){ next(); }else{ res.redirect("/login"); } }
works perfect logging in admin area, , checking if user logged in, when submit form '/admin/new' post route, browser hangs. nothing happens in console, console.log in place :
exports.create = function(req, res){ console.log(req); // database logic here res.redirect('/admin'); }
i cant seem work. hangs, , fails. browser console says 'pending' in network request.
ive tried removing 'authenticatedornot' method post route , same issue, if remove 3 works fine.
im stumped.
any guys? else run this?
i had problem similar this, i'm posting in case helps out. issue seemed had function definition inside passport function, , preventing done handler being called. think issue because when changed function argument names things started working.
in hindsight think error obvious, since i'm new node i'm still bit uncertain functions, callbacks, closures, etc, etc. have impression node convention use these argument names (err, done, next) , there magic associated them. guess not though. feel free educate me on point.
anyway, using passport local strategy copied tutorial (at http://scotch.io/tutorials/javascript/easy-node-authentication-setup-and-local). tutorial used mongo, decided switch postgresql. used pg.js module https://github.com/brianc/node-postgres-pure, , used sample code provided.
here's relevant portion of code, after copied , pasted pg.js sample code passport tutorial:
//bad code
passport.use('local', new localstrategy({ // default, local strategy uses username , password, override email usernamefield: 'email', passwordfield: 'password', passreqtocallback: true // allows pass entire request callback }, function(req, email, password, done) { pg.connect(configdb.connectionstring, function(err, client, done) { if (err) { return console.error('could not connect postgres', err); } client.query('select email, password_hash admin_user email = $1', [email], function(err, result) { // check password against db, , try call passports done callback return done(null, usermodel); // invokes pg.connect done callback }); }); }));
so when ran, on post /login, call done invoke pg.connect done, not passport done.
// good? working code
function(req, email, password, done) { pg.connect(configdb.connectionstring, function(err, client, connect_done) { if (err) { return console.error('could not connect postgres', err); } client.query('select email, password_hash admin_user email = $1', [email], function(err, result) { connect_done() // free postgres connection, should have been doing before // check password against db, , return done(null, usermodel); // invoke passport's done callback }); }); }));
this code working me (unless mis-copied something).
Comments
Post a Comment