Problem with Passport NodeJs authentication

I am using Passport Local for authentication. But whenever I try to login it is just using failureRedirect and not creating Cookie. I have checked my code so many times but it still does the same. What I have observed that my passport.js file is not executing for some resion.

My App Index File

//Main app index file
const express=require('express');
const cookieParser=require('cookie-parser');
const app=express();
const port=8000;
const layout=require('express-ejs-layouts');
const db=require('./config/mongoose');
const session=require('express-session');
const passport=require('passport');
const localStrategy=require('./config/passport-local-strategy');

app.use(express.urlencoded({ extended: true }));

app.use(cookieParser());

app.use(express.static('./assets'));

app.use(layout);

app.set('layout extractStyles',true);
app.set('layout extractScripts',true);

app.set('view engine','ejs');
app.set('views','./views');

app.use(session({
    name:'Codeial',
    secret:'Something',
    saveUninitialized:false,
    resave:false,
    cookie:{
        maxAge:(1000*60*100)
    }
}));

app.use(passport.initialize());
app.use(passport.session());

app.use('/',require('./routes'));

app.listen(port,function(err){
    if(err)
    {
        console.log(`Error:${err}`);
        return;
    }
    console.log(`Sever runs on port:${port}`);
});

My index router file

//index router file
const express=require('express');
const router=express.Router();

const control=require('../controller/router_controller');
const control2=require('../controller/user');

router.get('/',control.route);
router.use('/user',require('./users.js'));

console.log('Router is working');

module.exports=router;

User Router File

//user router file
const express=require('express');
const router=express.Router();
const passport=require('passport');
const pro=require('../controller/profile')
const user=require('../controller/user');

router.get('/profile',pro.profile);
router.get('/sign-in',user.sign_in);
router.get('/sign-up',user.sign_up);
router.post('/create',user.create);

router.post('/create-session',passport.authenticate('local',{failureRedirect:'/user/sign-in'}),user.createSession);

module.exports=router;

Passport and Passport local strategy file

//passport and passport local strategy
const passport=require('passport');
const LocalPassport=require('passport-local').Strategy;

const User=require('../model/user');

passport.use(new LocalPassport({usernameField:'email'},function(email,password,done){
    User.findOne({email:email},function(err,user){
        if(err)
        {
            console.log('Error while finding data for authentication');
            return done(err);
        }
        if(!user||user.password!=password)
        {
            return done(null,false);
        }
        return done(null,user);
    });
}));

passport.serializeUser(function(user,done){
    return done(null,done);
});

passport.deserializeUser(function(id,done){
    User.findById(id,function(err,user){
        if(err)
        {
            console.log('Error while finding data for authentication');
            return done(err);
        }
        return done(null,user);
    });
});

module.exports=passport;

79 thoughts on “Problem with Passport NodeJs authentication”

Leave a Comment