# koa-wechat-auth

> wechat auth

Latest version **1.4.3** (published 2018-11-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-wechat-auth
pnpm add koa-wechat-auth
yarn add koa-wechat-auth
bun add koa-wechat-auth
```

Provides the command `wxauth`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.3 |
| Published | 2018-11-24 |
| First published | 2018-06-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 86.9 KB |
| Known vulnerabilities | 0 (+26 in 2 direct dependencies) |
| Install scripts | no |
| Author | zheng_refn |
| Maintainers | zheng_refn |

## Links

- npm: https://www.npmjs.com/package/koa-wechat-auth
- npm.io page: https://npm.io/package/koa-wechat-auth

## Dependencies (9)

- [ejs](https://npm.io/package/ejs.md) ^2.5.9
- [koa](https://npm.io/package/koa.md) ^2.5.0
- [axios](https://npm.io/package/axios.md) ^0.18.0
- [nodemon](https://npm.io/package/nodemon.md) ^1.17.5
- [koa-views](https://npm.io/package/koa-views.md) ^6.1.4
- [jwt-simple](https://npm.io/package/jwt-simple.md) ^0.5.1
- [koa-router](https://npm.io/package/koa-router.md) ^7.4.0
- [koa-compress](https://npm.io/package/koa-compress.md) ^3.0.0
- [koa-bodyparser](https://npm.io/package/koa-bodyparser.md) ^4.2.0

## Recent versions

- 1.4.3 (latest) — 2018-11-24
- 1.4.2 — 2018-11-24
- 1.4.1 — 2018-09-06
- 1.4.0 — 2018-09-06
- 1.3.12 — 2018-09-05
- 1.3.11 — 2018-09-05
- 1.3.10 — 2018-09-04
- 1.3.9 — 2018-09-04
- 1.3.8 — 2018-08-21
- 1.3.7 — 2018-08-21
- 1.3.6 — 2018-08-21
- 1.3.5 — 2018-08-21
- 1.3.4 — 2018-08-07
- 2.0.0 — 2018-07-27
- 1.3.3 — 2018-07-23
- … 18 more at https://npm.io/package/koa-wechat-auth/versions

## README

一个微信授权的node小服务。

需要自己提供一个配置文件和一个首页html。

首先`npm install koa-wxauth`

然后在package.json里面的script写入
```javascript
"auth": "cross-env CONFIG=./config/auth.config.js PORT=80 supervisor ./node_modules/koa-wxauth"
```
注意此处的 `auth.config.js` 是自己配置的。给一个大概的参考配置：

```javascript
module.exports = {
  // 注册此服务的授权路由，此服务的controller为/auth/normal
  redirect_uri: "",
  // 授权方式
  scope: "snsapi_userinfo",
  // 首页html地址
  html_src: "./views/index.html",
  // 微信app信息
  wxApp: {
    appId: "",
    appSecret: "",
    appName: "",
    id: 0
  },
  // 鉴权，判断是否需要授权，true -> 不需要授权，false -> 需要授权
  authentication: ctx => {
    let token = ctx.cookies.get("token");
    return !!token;
  },
  // 生成token
  createTOKEN(wxApp, wxUser, user) {
    const JWTUtil = require("jwt-simple");
    const JWT_SECRET = "";
    let nowDate = Math.ceil(Date.now() / 1000);
    let expDate = nowDate + 30 * 24 * 60 * 60; //30天过期时间
    let data = {
      oid: wxUser.openid,
      sub: wxUser.unionid,
      app: wxApp.appName,
      exp: expDate,
      wx_app_id: wxApp.id,
      id: wxUser.user_id
    };
    return JWTUtil.encode(data, JWT_SECRET);
  },
  // 注入token
  injectTOKEN(ctx, token) {
    const domain = "";
    const path = "/";
    ctx.cookies.set("token", token, {
      domain: domain,
      path: path,
      expires: new Date(Date.now() + 604800000)
    });
  },
  // 获取到用户数据之后，希望做一发什么样的操作，比如将用户信息插入数据库什么的
  createUser(wxUser, wxApp) {}
};
```

---
_Source: https://npm.io/package/koa-wechat-auth · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
