0.9.7 • Published 9 years ago
koa-wxsession v0.9.7
koa-wxsession
Wechat session authorization and validation middleware for koa@2
Koa2
中间件采用 async/await 关键字, 所以使用环境必须使用 Node.js >= 7.0.0 的版本(暂时需要 --harmony_async_await 参数) 或者使用 babel
Installation
$ npm install koa-wxsessionExample
const Koa = require('koa')
const router = require('koa-router')()
const app = new Koa()
const wxsession = require('koa-wxsession')
const { authorization, validation } = wxsession({
appID: 'appID',
appSecret: 'appSecret',
// https://www.npmjs.com/package/redis#options-object-properties
redisConfig: {},
generator: code => { /* generator your session */ },
errors: {
'ERR_HEADER_MISSED': ctx => ctx.throw(400, 'an error')
}
})
router
.post('/auth', authorization, ctx => ctx.body = { session: ctx.state.wxinfo.session })
.get('/secret', validation, ctx => ctx.body = ctx.state.wxinfo)
app.use(router.routes())
app.listen(3000, _ => console.log('listening on port 3000'))Options
appID微信小程序的 appIDappSecret微信小程序的 appSecretstore存储 Session 的 自定义 store,详细格式请看lib/lru-cache.js和lib/redis.jsprefixstore 里 key 的前缀 wx-session:ttlSession 过期时间(毫秒) 2 60 60 * 1000redisConfig使用Redis作为 Session store,若不填则使用lru-cachestateKeyctx.state里存储的 key 的名字 wxinfogeneratorSession 生成函数 code => sha1(code)useEncryptedData替代rawData使用encryptedDatafalseerrorsERR_HEADER_MISSED缺少 header 时的回调函数ERR_SESSION_KEY_EXCHANGE_FAILEDcode错误或请求失败的回调函数ERR_UNTRUSTED_DATA请求的Data验证或解密失败的回调函数ERR_SESSION_INVALIDSession 验证失败的回调函数ERR_OTHERS其他错误的回调函数
License
MIT