How to end a session in ExpressJS

前端 未结 9 881
小鲜肉
小鲜肉 2020-12-12 15:48

I feel like this has to be buried somewhere in the documentation, but I can\'t find it.

How do you close or end or kill (whatever) a session in ExpressJS?

9条回答
  •  无人及你
    2020-12-12 15:50

    Session.destroy(callback)

    Destroys the session and will unset the req.session property. Once complete, the callback will be invoked.

    Secure way ↓ ✅

    req.session.destroy((err) => {
      res.redirect('/') // will always fire after session is destroyed
    })
    

    Unsecure way ↓ ❌

    req.logout();
    res.redirect('/') // can be called before logout is done
    

提交回复
热议问题