express-weapp-auth

0.2.0 • Public • Published

Express-weapp-auth

Build Status

Express middleware to decrypt wechat userInfo data for weapp login scenario.

Installation

# via Github
npm install xixilive/express-weapp-auth --save

# via npm
npm install express-weapp-auth --save

Usage

// basic example
import {middleware} from 'express-weapp-auth'
 
const app = require('express')()
app.post(
  '/session/:code',
 
  middleware('appId', 'appSecret'),
 
  (req, res, next) => {
    const {openId, sessionKey, userInfo} = req.weappAuth
    //your logic here
  }
)
 
// advance example
app.use(
  '/weapp/session/',
 
  middleware('appId', 'appSecret', (req) => {
    return req.body
  }, {dataKey: 'customDataKey'}),
  (req, res, next) => {
    const {openId, sessionKey, userInfo} = req.customDataKey
    //your logic here
  }
)

Middleware

// all arguments
middleware('appId', 'appSecret' [, paramsResolver, options])
 
// without optional arguments
middleware('appId', 'appSecret')
 
// without options argument
middleware('appId', 'appSecret' paramsResolver)
 
// without paramsResolver argument
middleware('appId', 'appSecret' options)

Arguments

  • appId: required, weapp app ID

  • appSecret: required, weapp app secret

  • paramsResolver: optional, a function(req){} to resolve auth-params for request object

  • options: optional, {dataKey: 'the key assign to req object to store decrypted data'}

ParamsResolver(req)

It will use a built-in default resolver to resolve params for request if there has no function passed to middleware function. and the default function resolves params in a certain priority:

  • req.body with the highest priority

  • req.query with middle priority

  • req.params with the lowest priority

And it expects the resolver function to return an object value with following structure:

{
  code: 'login code',
  rawData: 'rawData',
  signature: 'signature for rawData',
  encryptedData: 'encrypted userInfo',
  iv: 'cipher/decipher vector'
}

For more details about this, please visit 微信小程序 API

Package Sidebar

Install

npm i express-weapp-auth

Weekly Downloads

0

Version

0.2.0

License

MIT

Unpacked Size

26.9 kB

Total Files

25

Last publish

Collaborators

  • xxlive