When I request to logout a user, it successfully logs out the user and redirects them to the sign in page, but the session record of that user is still available in the mongofb session table. And again, when I click on logout button. It destroys the session and logs out the user. I don't know how I can fix this issue. It looks like the user is not getting logged out.
app.js
mongoose.connect(
'mongodb+srv://UserName:password@cluster0.orxsdfhy.mongodb.net/?retryWrites=true&w=majority',
{useNewUrlParser:true, useUnifiedTopology:true },
).then(()=>{})
.catch(error => console.log(error))
mongoose.Promise = global.Promise;
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser('secret'));
app.use(session({
secret: '7861',
resave: false,
saveUninitialized: true,
cookie: {
secure: true,
maxAge: 3000000 *60
},
store: MongoStore.create({
mongooseConnection: mongoose.connection,
mongoUrl:'mongodb+srv://UserName:password@cluster0.orxsdfhy.mongodb.net/?retryWrites=true&w=majority',
collection: 'session',
ttl: 28800
}),
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
require('./auth/passportAuthentication')(passport);
logOut.js
router.get("/logout", (req, res) => {
req.logout(err => {
if (err) {
return next(err);
}
req.session.destroy(function (err){
if(err){
console.log(err)
} else {
res.cookie("connect.sid",'')
return res.sendStatus(200)
}
}
}