title: Passing parameters in POST in Express with Passport
body: <p>I have made a small login application using Express and Passport. But it seems I can't direct the user into a specific userprofile. What has to happen is, the user fills up an HTML form, and he has to get redirected to his own profile page (For the sake of simplicity, lets say that page differs from other user pages only in terms of the title). I have this in my user routes. (i.e. routes/users.js)</p>
  
  <pre><code>router.get('/userprofile', authenticationMiddleware(), function (req, 
  res, next) {
      res.render('userprofile');
  });
  
  
  router.post('/login', passport.authenticate('local-login', {
          successRedirect: '/users/userprofile',
          failureRedirect: '/users/login',
          failureFlash: true
  }));
  </code></pre>
  
  <p>In my passport.js config file I have this.</p>
  
  <pre><code>passport.use('local-login', new LocalStrategy({
      usernameField: 'username',
      passwordField: 'password',
      passReqToCallback: true
  }, function (req, username, password, done) {
      db.pool.getConnection(function (err, connection) {
         if(err) throw err;
         var query = connection.query('SELECT * from users WHERE username = ?', username, function (err, rows) {
            if(err) return done(err);
            if(!rows.length) {
                return done(null, false, req.flash('loginMessage', 'No User Found'));
            }
            //Comparing passwords using bcrypt
             bcrypt.compare(password, rows[0].password, function(error, res){
                 if(res){
                     return done(null, rows[0]);
                 } else {
                     return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));
                 }
             });
         });
      });
  }));
  </code></pre>
  
  <p>What I need to do is get the username from the form, and pass it to the successRedirect attribute in passport.authenticate, and then I can modify the get method adding a route parameter like '/users/:username' and render 'username' view.</p>
  
  <p>How can I do this?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I did something like this.</p>
  
  <pre><code>router.get('/userprofile/:username', authenticationMiddleware(), function (req, res, next) {
      res.render('userprofile', {
          username: req.params.username,
          title: 'Welcome, '+ req.params.username
      });
  });
  
  
  router.post('/login', function (req, res, next) { //Testing callback.
      console.log("Username is: " + req.body.username);
      passport.authenticate('local-login', {
          successRedirect: '/users/userprofile/' + req.body.username, 
          failureRedirect: '/users/login',
          failureFlash: true
      })(req, res);    
      next();
  });
  </code></pre>
  
  <p>This works on some attempts. However, it returns this error sometimes.</p>
  
  <pre><code>POST /users/login 404 43.741 ms - 5226 /home/dasun/WebstormProjects/schoolentry/node_modules/passport/lib/middleware/authenticate.js:249
        if (err) { return next(err)}; 
  </code></pre>
  
  <p>How can I get rid of this error?</p>
  
  <p>Thanks.</p>