How to access POST data on node.js passport authentication?

Manuel Bieh Source

I'm trying to access POST parameters after submitting a login form with passport. My form is as follows:

<form method="post">
    <input name="username">
    <input name="password">
    <input type="checkbox" name="remember" value="1">
    <input type="submit">

The (working) express route/callback:
    passport.authenticate('local', {
        failureRedirect: '/login', 
        failureFlash: true, 
        badRequestMessage: 'Please enter your account credentials to login.'
    function(req, res) {
        if(req.isAuthenticated(req, res)) {
        } else {
            var errors = req.flash('error');
            if(errors) {
                assign['errors'] = errors;
            res.render('login.html', {errors: errors});

Login works fine, everything cool. BUT: req.param('remember') is always undefined. When I remove the passport.authenticate() part, check the checkbox in my form and submit the form console correctly logs 1.

So how can I access the POST parameters when I'm also using passport.authenticate()?



answered 6 years ago zemirco #1

Haven't used passport so far but here are two things that might cause your problem

1. Your form doesn't have an action attribute

Therefore the form doesn't know where to send the data. Try the following

<form method="post" action="/login">
  <input name="username">
  <input name="password">
  <input type="checkbox" name="remember" value="1">
  <input type="submit">

2. POST variables in express are attached to the req.body object

So instead of




Make sure you have the bodyParser in your express config.

req.param is used when you want to access dynamic routes

app.get('/login/:user', function(req, res) {

// GET /login/john => 'john'

comments powered by Disqus