Code Monkey home page Code Monkey logo

toa-session's Introduction

toa-session

Session middleware for toa, inspired by generic-session.

NPM version Build Status Downloads

Demo

use as middleware:

const Toa = require('toa')
const session = require('toa-session')()

const app = new Toa()
app.use(function () {
  if (this.path === '/favicon.ico') return
  if (this.path === '/delete') this.session = null
  else this.session.name = 'test'

  this.body = {
    path: this.path,
    session: this.session,
    sessionId: this.sessionId
  }
})

app.use(session)
app.listen(3000)

use as module:

const Toa = require('toa')
const session = require('toa-session')()

const app = new Toa()(function *() {
  if (this.path === '/favicon.ico') return
  yield session

  if (this.path === '/delete') this.session = null
  else this.session.name = 'test'

  this.body = {
    path: this.path,
    session: this.session,
    sessionId: this.sessionId
  }
})

app.listen(3000)
  • After adding session middleware, you can use this.session to set or get the sessions.
  • Setting this.session = null; will destroy this session.

Installation

npm install toa-session

API

const session = require('toa-session');

app.use(session([options]))

  • options.key: String, cookie name, default to toa.sid.

  • options.store: object, session store instance.

  • options.ttl: Number, store ttl in ms, default to 24 * 60 * 60 * 1000.

  • options.prefix: String, session prefix for store, default to toa:sess:.

  • options.cookie: Object, session cookie settings.

  • options.rolling: Boolean, rolling session, always reset the cookie and sessions, default to false.

  • options.sidSize: Number, random bytes's length to generate sid, sid included timestamp hash and CRC bytes, so it's length is long than sidSize, default to 24.

  • options.genSid: Function, you can use your own generator for sid, default to ./lib/sid.js.

  • Store can be any Object that has the methods set, get, destroy like memoryStore.

  • cookie defaulting to

const defaultCookie = {
  httpOnly: true,
  path: '/',
  overwrite: true,
  signed: true,
  maxAge: 24 * 60 * 60 * 1000 // ms
};

Session Store

You can use any other store to replace the default MemoryStore, it just needs to follow this api:

  • get(sid): get session object by sid
  • set(sid, session, ttl): set session object for sid, with a ttl (in ms)
  • destroy(sid): destory session for sid

the api needs to return a Promise, Thunk or generator.

And use these events to report the store's status.

  • connect
  • disconnect

Licences

(The MIT License)

toa-session's People

Contributors

zensh avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.