P
P
partyzanx2020-08-29 14:51:45
Node.js
partyzanx, 2020-08-29 14:51:45

How not to set expires for session in express?

When a user enters the site, he checks the box "stay logged in after closing the site" or does not check it.
How can I not set expires, max-age for the session, so that when all tabs of the site are closed, the session is closed?

I have such a code that is triggered when logging into the site.

router.post('/signin', isAuth, loginValidators, async (req, res) => {
    const lang = req.cookies.lang;
    try {
        const email = sanitize(req.body.email).trim()
        const password = req.body.password 
        const remainInSystem = sanitize(req.body.remainInSystem) 

        const candidate = await User.findOne({email})

        if (candidate) {
            if (!password) {
                req.flash('error', translatingsFlashes.invalid_current_password[lang]);
                return res.redirect('/settings');
            }
            if (!candidate.password) {
                req.flash('error', translatingsFlashes.invalid_current_password[lang]);
                return res.redirect('/settings');
            }
            const areSame = await bcrypt.compare(password, candidate.password)
            if (areSame) { 
                if (remainInSystem == 'on') {
                    await User.findOneAndUpdate({email}) 
                } else {
                    await User.findOneAndUpdate({email}) 
                }
                
                // Delete previous sessions of the user

                candidate.lastSignedIn = Date.now(); 
                req.session.user = candidate;
                req.session.isAuthenticated = true;
                req.session.save(err => {
                    if (err) {
                        req.flash('error', err)
                        res.redirect('/user/signin')
                    }
                    res.redirect(`/user/${candidate._id}`)
                })
            } else {
                req.flash('error', translatingsFlashes.password_is_incorrect[lang])
                res.redirect('/user/signin')
            }
        } else {
            req.flash('error', translatingsFlashes.no_such_user_with_that_email_please_sign_up[lang])
            res.redirect(`/user/signup?email=${email}`)
        }
    } catch (error) {
        console.log(error);
    }
})

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