@midwayjs/session
v3.19.0
Published
midway session component for koa and faas
Downloads
5,395
Readme
@midwayjs/session
Session component for @midwayjs/koa and @midwayjs/faas
Install
$ npm i @midwayjs/session --save
Usage
@midwayjs/koa has enabled this component by default, @midwayjs/faas needs to be manually enabled.
// src/configuration.ts
import { join } from 'path';
import * as faas from '@midwayjs/faas';
import * as session from '@midwayjs/session';
@Configuration({
imports: [
faas,
session,
],
// ...
})
export class ContainerLifeCycle implements ILifeCycle {}
Config
You can configure session in your config.*.ts
.
default value.
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.
import { SessionStore } from '@midwayjs/session';
@Provide()
@Scope(ScopeEnum.Singleton)
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.
import { MemorySessionStore } from './store';
import * as session from '@midwayjs/session';
@Configuration({
imports: [
koa,
session,
],
//...
})
export class AutoConfiguration {
@Inject()
memoryStore: MemorySessionStore;
@Inject()
sessionStoreManager: session.SessionStoreManager;
async onReady() {
this.sessionStoreManager.setSessionStore(this.memoryStore);
}
}
Questions & Suggestions
Please open an issue here.
License
MIT