express-weapp-auth
v0.2.0
Published
Express middleware to decrypt wechat userInfo data for weapp(微信小程序) login scenario.
Downloads
4
Maintainers
Readme
Express-weapp-auth
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 IDappSecret
: required, weapp app secretparamsResolver
: optional, afunction(req){}
to resolve auth-params for request objectoptions
: 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 priorityreq.query
with middle priorityreq.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