Q:

`Bad Request` error: while using passport.js + express

I have the following code to redirect valid users to /profile url with passport.js package, but I'm receiving Bad Request error, the console logs: GET /profile 400 24.100 ms - - 
I wonder what might be the cause.
var express = require('express')

//...

app.post('/login',
    passport.authenticate('local', {
        successRedirect: '/profile',
        failureRedirect: '/',
        failureFlash: true
    })
);

var morgan=require('morgan')
app.use(morgan('dev')) // log every request to the console

app.get('/profile', passport.authenticate('local'),function(req,res){
    res.render('profile.ejs',{
        user: req.user
    });
})

A:

My problem was due to not including cookie-session in my code. I added the following lines to my code and the problem got solved.
var cookieSession=require('cookie-session')
app.use(cookieSession({
    keys:['secret1', 'secret2']
}))

Apparently passport.js should be used along with cookie-session to be able to store user session state in browser cookie.
Also, I had to change the code as follows:
function authenticationMiddleware(){
    return function(req,res,next){
        if(req.isAuthenticated()){
            return next()
        }
        res.redirect('/')
    }
}

app.get('/profile', authenticationMiddleware(), function(req, res) {
////the following line did NOT work, gave `Bad Request` error:
//app.get('/profile', passport.authenticate('local'),function(req,res){
    res.render('profile.ejs',{
        user: req.user
    });
})

This and that GitHub files helped me to write the authenticationMiddleware()
UPDATE
Later I realized that I don't need to use a middle-ware. Now my code works without a middle-ware, like this:
app.get('/profile',(req,res)=>{
    res.render('profile.ejs',{
        user: req.user
    });
})

