JSPM

  • Created
  • Published
  • Downloads 1180
  • Score
    100M100P100Q114043F
  • License MIT

midway session component for koa and faas

Package Exports

  • @midwayjs/session
  • @midwayjs/session/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@midwayjs/session) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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