使用 Loopback 滑动访问令牌的到期时间

Sliding expiration for access token with Loopback

我已经为几个型号安装了 Loopback and enabled ACL。我注意到 A​​ccess Token 永远有效,我想以某种方式将此时间段更改为例如一个小时。但更好的方法是在 activity 发生(滑动到期)

时重置此期间

我已经检查了 documentation,但对这个主题一无所知。任何 help/guidance 将不胜感激!

当您调用登录方法时,您可以specify a ttl property in seconds(如果您不指定,我相信默认情况下是 2 周)。然后你可以通过使用以下中间件来滑动过期:

app.use(loopback.token()); // You should have this already
app.use(function(req, res, next) {
    // Make sure this middleware is registered after loopback.token
    var token = req.accessToken;
    if (!token) {
        return next();
    }
    var now = new Date();
    if ( now.getTime() - token.created.getTime() < 1000 ) {
        return next();
    }
    req.accessToken.created = now;
    req.accessToken.ttl     = 604800; //one week
    req.accessToken.save(next);
});