javascript - Node.js Passport not calling next function -
i'm building application using node uses passport.js handle user login using local database.
so have following code gets called when user goes /profile. after logging in user gets redirected /profile. happen according morgan.
app.get('/profile', passport.authenticate('local-login', { session : false, failureredirect : '/login' }), function(req, res) { console.log("testnow"); res.render('profile.ejs', { user : req.user // user out of session , pass template }); });
my local-login code following.
passport.use('local-login', 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) { // callback email , password our form // find user email same forms email // checking see if user trying login exists user.findone({ 'local.email' : email }, function(err, user) { // if there errors, return error before else if (err) return done(err); // if no user found, return message if (!user) return done(null, false, req.flash('loginmessage', 'no user found.')); // req.flash way set flashdata using connect-flash // if user found password wrong if (!user.validpassword(password)) return done(null, false, req.flash('loginmessage', 'oops! wrong password.')); // create loginmessage , save session flashdata // well, return successful user console.log("testdone"); return done(null, user); }); }));
when testing code login , redirected profile split second. console prints "testdone" in local-login code doesn't print "testnow" expected to. meaning second function in /profile method never seems called tho local-login calling next function.
so end users standpoint login (behind scenes redirected /profile split section) , /profile redirects /login.
any ideas on how fix second function in /profile method gets called?
thanks in advance. more happy provide additional information figure out.
passport.authenticate()
meant handle actual authentication; in other words, take login credentials , pass them strategy. it's not meant pass along requests if authenticated, you're trying use for.
instead, want use connect-ensure-login
guard routes user has logged in.
Comments
Post a Comment