P
P
partyzanx2020-08-30 14:51:20
Node.js
partyzanx, 2020-08-30 14:51:20

How to delete a session after closing the browser?

Hey! I use

const session = require('express-session');
const MongoStore = require('connect-mongodb-session')(session);

const store = new MongoStore({
  collection: 'sessions',
  uri: process.env.MONGODB_URI
})

app.use(session({
  secret: process.env.SECRET,
  resave: false,
  saveUninitialized: false,
  store,
}))


I want that when the user logs in and ticks "do not stay on the site", then the session closes when the browser is closed, i.e. breakdown took place.

I do this but it doesn't help.

router.post('/signin', async (req, res) => {
 //... 
                req.session.user = candidate;
                req.session.cookie.expires = false; 
                req.session.isAuthenticated = true;
                req.session.save(err => {
                    if (err) {
                        req.flash('error', err)
                        res.redirect('/user/signin')
                    } 
//...
})


With this, req.session.cookie =
{
  path: '/',
  _expires: false,
  originalMaxAge: false,
  httpOnly: true,
  secure: null,
  domain: null,
  sameSite: null
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question