koa-jwt-session

0.1.2 • Public • Published

JWT (JSON Web Token) Session Middleware for Koa

NPM version build status Test coverage npm download

Barebone JWT based session for Koa 2. Tests and configurable options comming soon.

The API is exposed on the context object (this) inside downstream middlewares.

Getting started

import JWTSession from 'koa-jwt-session';
import Koa from 'koa';
const app = new Koa();
app.use(jwtAuth(app, {
  secret: "Shhhha",
  signed: false, // Default to false
  httpOnly: false, // Default to false
  domain: process.env.SESSIONS_SCOPE // Cookie scope. Must be set
}));
 
// Generate a Session from a controller (route handler)
async function(ctx, next) {
  const formFields = await parse(ctx)
  // store session
  const data = ctx.JWTSession.generate({_id: formFields._id, nickname: formFields.nickname});
}
 
// Validating a session from a controller (route handler)
async function(ctx, next) {
  const authed = ctx.JWTSession.authed();
  if ( authed ) {
    // Do something that requires auth
  } else {
    // Handle error
  }

API

  • generate(payload)
  • get()
  • set()
  • clear()
  • authed()

Package Sidebar

Install

npm i koa-jwt-session

Weekly Downloads

0

Version

0.1.2

License

MIT

Last publish

Collaborators

  • 0mbre