2.0.0 • Published 6 years ago
iox-stateless v2.0.0
iox-stateless
stateless token for api
Versions
- 2.0.0 Removed support for web
Installation
npm install iox-stateless --save
Compatible
Options
Options to initialize the framework
secret (String)
The jwt secret key
expire (Number, time in seconds) (Optional)
The token expiration
Default value is 1 hour (60 * 60)
isBearer (Boolean) (Optional)
Parse bearer token
Default value is true
header (String) (Optional)
The name of variable in header
Default value is 'authorization'
Attention
- Token must be sent via header for compatibility between http methods.
- Set all variables in one route to generate token
- If you set a variable using set, the current body will be modified by passing the token
- Don’t save sensitive data, jwt token is not encrypted.
API context
/**
* @param name (String) the key
* @param value (any) the object value
*
* Body return if use this
* {
* "success": true,
* "token": "you token generated"
* }
*/
ctx.stateless.set(name, value)
/**
* @param name (String) the key
* @return value || udefined
*/
ctx.stateless.get(name)
Usage
const Koa = require('koa')
const Router = require('koa-router')
const Stateless = require('iox-stateless')
// Setup the koa and router
const app = new Koa()
const router = new Router()
// setup in middleware
router.use(Stateless({
secret: 'you secret', // jwt secret
}))
// set the stateless data
router.post('/set', (ctx) => {
// set the stateless value
ctx.stateless.set('id', 2019)
})
// get the stateless data
router.post('/get', (ctx) => {
// get the stateless value
const id = ctx.stateless.get('id')
// sample body response
ctx.body = 'hello world'
})
app.use(router.routes())
app.use(router.allowedMethods())
// Start the server
app.listen(process.env.PORT || 8080)