title: req.flash is not a function express passport
body: <p>I am trying to authenticate the user using passport.I am trying to display message using flash.In the passport-config.js,I wrote the local strategy.When I run the code ,it give an error  &quot;req.flash is not a function &quot; in passport-config.js file.This the first time I am using passport and flash.Please help me.Thanks in advance.
  app.js</p>
  <pre><code>const express = require(&quot;express&quot;);
  const bodyParser = require(&quot;body-parser&quot;);
  const ejs = require(&quot;ejs&quot;);
  const passport = require(&quot;passport&quot;);
  const initializePassport=require('./passport-config')(passport);
  const auth=require('./authenticate');
  const bcrypt=require('bcrypt');
  const session = require('express-session');
  const flash = require('express-flash');
  const Post=require(&quot;./Post&quot;);
  const User=require('./User');
  const app = express();
  app.set('view engine', 'ejs');
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({extended: true}));
  app.use(express.static(&quot;public&quot;));
  app.use(session({
    secret:&quot;secret&quot;,
    resave:false, //this means session variables are not resaved if nothing is changed
    saveUninitialized:false //this means we dont want to save empty value in session if there is no value
  }))
  app.use(flash());
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(function(req, res, next) {
    // make all error and success flash messages available from all templates
    res.locals.error = req.flash(&quot;error&quot;)
    res.locals.success = req.flash(&quot;success&quot;)
    next()
  })
  
  app.get(&quot;/login&quot;,function(req,res){
    res.render('login');
  })
  
  app.post(&quot;/login&quot;,passport.authenticate('local',{
    successRedirect:'/',
    failureRedirect:'/login',
    failureFlash:true
  }))
  </code></pre>
  <p>passport-config.js</p>
  <pre><code>const LocalStrategy = require('passport-local').Strategy;
  const mongoose=require('mongoose');
  const bcrypt = require('bcrypt');
  const User=require('./User');
  function initialize(passport) {
    const authenticateUser =(req,email, password, done) =&gt; {
      User.findOne({email:email}).then(user=&gt;{
        if(!user){
        //   req.flash('error','This email is not registered');
          return done(null,false,req.flash('error','This email is not registered'));
        }
        //Match password
        bcrypt.compare(password,user.password,(err,isMatch)=&gt;{
          if(err){
             //  req.flash('error','Something went wrong!Please try again.');
            throw err;
          }
          if(isMatch){
            return done(null,user); 
          }
          else{
             req.flash('error','Password Incorrect');
            return done(null,false, req.flash('error','Password Incorrect'));
          }
        });
      }).catch(err=&gt; console.log(err));
    }
  
    passport.use(new LocalStrategy({ usernameField: 'email' ,passReqToCallBack: true}, authenticateUser));
    passport.serializeUser((user, done) =&gt; done(null, user.id));
    passport.deserializeUser((id, done) =&gt; {
      User.findById(id,(err,user)=&gt;{
      done(err,user);
      })
    })
  }
  
  module.exports = initialize;
  </code></pre>