Skip to content

Commit

Permalink
docs: intro session.renew in the doc (#2375)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Apr 12, 2018
1 parent f0e7773 commit 5f2358b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
24 changes: 6 additions & 18 deletions docs/source/en/core/cookie-and-session.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,27 +248,15 @@ class UserController extends Controller {

By default, if user requests don't result in modification of Session,
egg.js doesn't extend expiration time of the session.
But in some scenarios, you may need to refresh expiration time every time user access the website,
so that users will only be logged out when they don't access website for long time.
This requirement can be done through `ctx.session.save()`.

For example, we create a middleware in the application.
It forces saving session in every request, in order to extend session's expiration time.
But in some scenarios, we hope that if users visit our site for a long time, then extend their session validity and not let the user exit the login state. The framework provides a `renew` configuration item to implement this feature. It will reset the session's validity period when it finds that the user's session is half the maximum validity period.

```js
// app/middleware/save_session.js
module.exports = () => {
return async function saveSession(ctx, next) {
await next();
// if Session is empty, do nothing
if (!ctx.session.populated) return;
ctx.session.save();
};
};

// config/config.default.js
// import the middleware in config file
exports.middleware = [ 'saveSession' ];
module.exports = {
session: {
renew: true,
},
};
```

[egg-redis]: https://github.com/eggjs/egg-redis
Expand Down
21 changes: 6 additions & 15 deletions docs/source/zh-cn/core/cookie-and-session.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,15 @@ class UserController extends Controller {

#### 延长用户 Session 有效期

默认情况下,当用户请求没有导致 Session 被修改时,框架都不会延长 Session 的有效期,但是在有些场景下,我们希望用户每次访问都刷新 Session 的有效时间,这样用户只有在长期未访问我们的网站的时候才会被登出。这个功能我们可以通过 `ctx.session.save()` 来实现。

例如,我们在项目中增加一个中间件,让它在 Session 有值的时候强制保存一次,以达到延长 Session 有效期的目的。
默认情况下,当用户请求没有导致 Session 被修改时,框架都不会延长 Session 的有效期,但是在有些场景下,我们希望用户如果长时间都在访问我们的站点,则延长他们的 Session 有效期,不让用户退出登录态。框架提供了一个 `renew` 配置项用于实现此功能,它会在发现当用户 Session 的有效期仅剩下最大有效期一半的时候,重置 Session 的有效期。

```js
// app/middleware/save_session.js
module.exports = () => {
return async function saveSession(ctx, next) {
await next();
// 如果 Session 是空的,则不保存
if (!ctx.session.populated) return;
ctx.session.save();
};
};

// config/config.default.js
// 在配置文件中引入中间件
exports.middleware = [ 'saveSession' ];
module.exports = {
session: {
renew: true,
},
};
```

[egg-redis]: https://github.com/eggjs/egg-redis
Expand Down

0 comments on commit 5f2358b

Please sign in to comment.