title: Using passport in nodejs without redirect and display message on same route
body: <p>I am using nodejs to create restful api without jwt just using cookie for authentication.
  Below is sample code with hardcoded data : </p>
  
  <pre><code>            var express=require('express');
              var app=express();
              var bodyparser=require('body-parser');
              var passport=require('passport');
              var morgan=require('morgan');
              var cookieParser = require('cookie-parser');
              var session      = require('express-session');
  
  
              app.use(bodyparser.urlencoded({extended:false}));
              app.use(bodyparser.json());
              app.use(morgan('dev'));
              app.use(session({ secret: 'testing',resave: true,saveUninitialized: true }));
              app.use(passport.initialize());
              app.use(passport.session());
  
  
              var LocalStrategy = require('passport-local').Strategy;
              passport.use(new LocalStrategy(function(username,password,done){
  
                if(username=="admin" &amp;&amp; password=="admin"){
                         return done(null,{id:203,name:'admin',email:'admin@admin.com'});
                }  
                else{
                  return done(null,false,{"message": "Invalid Username / Password"});
                }
  
              }));
  
              passport.serializeUser(function (user, done) {
                  done(null,203);
              });
              passport.deserializeUser(function (id, done) {
                  done(null,{id:203,name:'admin',email:'admin@admin.com'} );
              });
  
              function isLoggedIn(req, res, next) {
                  if (req.isAuthenticated())
                      return next();
  
                  res.sendStatus(401);
              }
  
  
              app.get('/',function(req,res){
               res.send({'status':'in Index'});
              });
  
              app.get('/login',
                  passport.authenticate('local',{successRedirect:'/dashboard',failureRedirect:'/loginfailed'}),
                  function(req,res){
                  console.log('In Login');    
                  res.send(req.session);
              });
  
              app.get('/loginfailed',function(req,res){
  
               res.send({'status':'login failed'});
              });
  
              app.get('/dashboard',isLoggedIn,function(req,res){
              console.log(req.user.id);   
              console.log(req.session.passport);
               res.send({'status':'login Success'});
              });
  
              app.get('/logout',function(req,res){
                  req.logOut();
                  req.session.destroy(function (err) {
                      res.clearCookie('connect.sid');
                      res.redirect('/'); 
                  });
  
              });
  
  
              app.listen(5000);
  </code></pre>
  
  <p>Everthing is working fine but I don’t want to redirect user to different route  on successful login or on failure just show any message on same route.
  Is it possible ? If possible what the right way to do it ?
  Any help would be appreciated. </p>