根据https://github.com/mozilla/node-client-sessions#usage

var sessions = require("client-sessions");
app.use(sessions({
  cookieName: 'mySession', // cookie name dictates the key name added to the request object
  secret: 'blargadeeblargblarg', // should be a large unguessable string
  duration: 24 * 60 * 60 * 1000, // how long the session will stay valid in ms
  activeDuration: 1000 * 60 * 5 // if expiresIn < activeDuration, the session will be extended by activeDuration milliseconds
}));

我希望用户的 session 最多可以持续30天。如果我将duration设置为30天,并将activeDuration设置为30天,我认为我将以60天为期的 session 结束。

如果我将duration设置为15天,并将activeDuration设置为15天,那么在15天之内登录的用户将不会退出,然后什么也不退出(这不是我期望的那种用户行为,但是我的假设正确吗? )

我想要的是让用户每次进入网站至少需要30天才能重新登录。我如何获得这种行为?

最佳答案

您可以使用快速 session NPM来实现您的功能:

这是代码和引用链接:

CODE:

let session = require('express-session')
let time = new Date(Date.now() + (30 * 86400 * 1000))
let app = express()
let sess = {
  secret: 'keyboard_cat',
  cookie: { maxAge: time }
}

if (app.get('env') === 'production') {
  app.set('trust proxy', 1) // trust first proxy
  sess.cookie.secure = true // serve secure cookies
}

app.use(session(sess))

app.use(session({
  genid: function(req) {
    return genuuid() // use UUIDs for session IDs
  },
  secret: 'keyboard cat'
}))

//call your route and do session things here
app.get('/', function(req, res, next) {
  if (req.session.views) {
    req.session.views++
    res.setHeader('Content-Type', 'text/html')
    res.write('<p>views: ' + req.session.views + '</p>')
    res.write('<p>expires in: ' + (req.session.cookie.maxAge / 1000) + 's</p>')
    res.end()
  } else {
    req.session.views = 1
    res.end('welcome to the session demo. refresh!')
  }
})

有关更多信息,请参见以下链接:

NPM package

example

10-02 09:14