Giter VIP home page Giter VIP logo

koa-wxsession's Introduction

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-wxsession

Example

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 微信小程序的 appID
  • appSecret 微信小程序的 appSecret
  • store 存储 Session 的 自定义 store,详细格式请看 lib/lru-cache.jslib/redis.js
  • prefix store 里 key 的前缀 [wx-session:]
  • ttl Session 过期时间(毫秒) [2 * 60 * 60 * 1000]
  • redisConfig 使用 Redis 作为 Session store,若不填则使用 lru-cache
  • stateKey ctx.state 里存储的 key 的名字 [wxinfo]
  • generator Session 生成函数 [code => sha1(code)]
  • useEncryptedData 替代 rawData 使用 encryptedData [false]
  • errors
    • ERR_HEADER_MISSED 缺少 header 时的回调函数
    • ERR_SESSION_KEY_EXCHANGE_FAILED code 错误或请求失败的回调函数
    • ERR_UNTRUSTED_DATA 请求的 Data 验证或解密失败的回调函数
    • ERR_SESSION_INVALID Session 验证失败的回调函数
    • ERR_OTHERS 其他错误的回调函数

License

MIT

koa-wxsession's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

sunsnow8

koa-wxsession's Issues

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.