Skip to content

Latest commit

 

History

History
executable file
·
62 lines (51 loc) · 1.5 KB

README.md

File metadata and controls

executable file
·
62 lines (51 loc) · 1.5 KB

claypot-wxapp-auth-plugin

Build Status

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