1.4.3 • Published 5 years ago

koa-wechat-auth v1.4.3

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

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

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

首先npm install koa-wxauth

然后在package.json里面的script写入

"auth": "cross-env CONFIG=./config/auth.config.js PORT=80 supervisor ./node_modules/koa-wxauth"

注意此处的 auth.config.js 是自己配置的。给一个大概的参考配置:

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) {}
};
1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.12

6 years ago

1.3.11

6 years ago

1.3.10

6 years ago

1.3.9

6 years ago

1.3.8

6 years ago

1.3.7

6 years ago

1.3.6

6 years ago

1.3.5

6 years ago

1.3.4

6 years ago

2.0.0

6 years ago

1.3.3

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago