@midwayjs/session
Version:
midway session component for koa and faas
112 lines (80 loc) • 1.84 kB
Markdown
# /session
Session component for /koa and /faas
## Install
```bash
$ npm i /session --save
```
## Usage
/koa has enabled this component by default, /faas needs to be manually enabled.
```bash
// src/configuration.ts
import { join } from 'path';
import * as faas from '@midwayjs/faas';
import * as session from '@midwayjs/session';
export class ContainerLifeCycle implements ILifeCycle {}
```
## Config
You can configure session in your `config.*.ts`.
default value.
```ts
export const session = {
maxAge: 24 * 3600 * 1000, // ms
key: 'mw.sess',
httpOnly: true,
encrypt: true,
// sameSite: null,
logValue: true,
};
```
you can use all config from koa-session.
## Custom Session Store
Write a session store class first, extends abstract `SessionStore` class.
```ts
import { SessionStore } from '@midwayjs/session';
export class MemorySessionStore extends SessionStore {
sessions = {};
async get(key) {
return this.sessions[key];
}
async set(key, value) {
this.sessions[key] = value;
}
async destroy(key) {
this.sessions[key] = undefined;
}
}
```
add custom sessionStore to session component.
```ts
import { MemorySessionStore } from './store';
import * as session from '@midwayjs/session';
export class AutoConfiguration {
memoryStore: MemorySessionStore;
sessionStoreManager: session.SessionStoreManager;
async onReady() {
this.sessionStoreManager.setSessionStore(this.memoryStore);
}
}
```
## Questions & Suggestions
Please open an issue [here](https://github.com/midwayjs/midway/issues/).
## License
MIT