using request.flash show login error message

The ideal condition is while in route ‘/login/users’, showing ‘Please login first’. And after typing, if eamil or password is not suitable, showing ‘This email is not registered!’ or ‘Email or Password is incorrect. But, Not knowing how to make it work. The real condition only shows ‘Please login first’ on every condition above.

I try to rewrite req.flash(‘errors’) as req.flash(‘errors’, ‘ ‘). Error shows with two commas in the begining and the end. But it comes another problem: while in this route ‘/login/users’, Error seciton still shows with no content.

How to display login error with req.flash or fix the problem of Error section?

app.js

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: false,
  saveUninitialized: true
}))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static('public'))
app.use(methodOverride('_method'))

usePassport(app)
app.use(flash())
app.use((req, res, next) => {
  res.locals.isAuthenticated = req.isAuthenticated()
  res.locals.user = req.user
  res.locals.success_msg = req.flash('success_msg')
  res.locals.warning_msg = req.flash('warning_msg')
  res.locals.error = req.flash('error', ' ')
  res.locals.errors = req.flash('errors')
  next()
})
app.use(routes)

congfig/passport.js

module.exports = app => {
  app.use(passport.initialize())
  app.use(passport.session())
  passport.use(new LocalStrategy({
    usernameField: 'email',
    passReqToCallback: true,
  }, (req, email, password, done) => {
    User.findOne({ email })
      .then(user => {
        if (!user) {
          return done(null, false, req.flash('error', 'This email is not registed!'))
        }
        return bcrypt.compare(password, user.password)
          .then(isMatch => {
            if (!isMatch) {
              return done(null, false, req.flash('error', 'Email or Password incorrect.'))
            }
            return done(null, user)
          })
          .catch(err => done(err, false))
      })
  }))

middleware/auth.js


module.exports = {
  authenticator: (req, res, next) => {
    if (req.isAuthenticated()) {
      return next()
    }
    req.flash('warning_msg', 'Please login first!')
    res.redirect('/users/login')
  }
}

routes/models/user.js

router.get('/login', (req, res) => {
  res.render('login', { error: req.flash('error') })
})

router.get('/register', (req, res) => {
  res.render('register')
})

router.post('/login', passport.authenticate('local', {
  successRedirect: '/',
  failureRedirect: '/uses/login',
  failureFlash: true,
}))

The ideal condition is while in route ‘/login/users’, showing ‘Please login first’. And after typing, if eamil or password is not suitable, showing ‘This email is not registered!’ or ‘Email or Password is incorrect. But, Not knowing how to make it work. The real condition only shows ‘Please login first’ on every condition above.

I try to rewrite req.flash(‘errors’) as req.flash(‘errors’, ‘ ‘). Error shows with two commas in the begining and the end. But it comes another problem: while in this route ‘/login/users’, Error seciton still shows with no content.

How to display login error with req.flash or fix the problem of Error section?