koa-jwt-session
v0.1.2
Published
Barebone JWT Session management for Koa
Downloads
12
Readme
JWT (JSON Web Token) Session Middleware for Koa
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()