claypot-wxapp-auth-plugin
v0.1.0
Published
claypot plugin for wechat mini program login and get user info
Downloads
1
Readme
claypot-wxapp-auth-plugin
Wechat mini program auth plugin for Claypot, built on top of wxapp-auth.
Installing
$ yarn add claypot claypot-restful-plugin claypot-wxapp-auth-plugin
Usage and Example
Claypotfile.js
module.exports = {
plugins: [
{
module: 'claypot-restful-plugin',
options: {},
},
{
module: 'claypot-wxapp-auth-plugin',
options: {
appId: '<APPID>', // required
appSecret: '<APP SECRET>', // required
namespace: 'wxappAuth', // `ctx.clay.wxappAuth`
prefix: 'wxappAuth', // cache key prefix
ttl = 172800, // cache ttl (2 days by default)
security: 'wxapp', // `claypot-restful-plugin` security
signKey: 'id', // sign({ [signKey]: signValue })
},
},
],
};
models/User.js
export default class Users {
async login({ body }, ctx) {
const { code } = body;
const { wxappAuth } = ctx.clay;
const { openid, sign } = await wxappAuth.login({ code });
const { accessToken, expiresIn } = await sign({ id: openid });
return { accessToken, expiresIn };
}
async getUserInfo({ body }, ctx) {
const { wxappAuth } = ctx.clay;
const userInfo = await wxappAuth.getUserInfo(body);
return userInfo;
}
// ...
}
License
MIT