Giter VIP home page Giter VIP logo

meteor-accounts-wechat-mp's Introduction

zhaoyao91:accounts-wechat-mp

Meteor accounts package for wechat-mp. Because this package is generally used in China, this doc will be written in chinese.

简介

用法

1. 添加包

meteor add zhaoyao91:accounts-wechat-mp
meteor add zhaoyao91:short-oauth-state
meteor add service-configuration

2. 配置

server端:

ServiceConfiguration.configurations.upsert({
    service: WeChatMP.serviceName // 可以通过Meteor.settings.public.wechatMPServiceName来修改这个值
}, {
    $set: {
        appId: '...',
        secret: '...',
        scope: 'base_userinfo',
        loginStyle: 'redirect',
        mainId: 'openId'
    }
});

3. 登录

client端:

Meteor.loginWithWeChatMP(function(err, res){
   ... 
})

注意,本服务仅能在微信浏览器中使用,且只能按redirect的方式打开

如何绑定微信开放平台登录?

微信开放平台和微信公众平台对于meteor来说,是两个不同的服务,对微信来说,登录的接口也有所不同。所以如果一个meteor同时支持开放 平台登录和公众平台登录,那么同一微信用户若使用两种不同的方式登录,会分别进入到不同的meteor账号。

微信提供了unionId机制来打通开放平台和公众平台(详情请查看微信相关文档)。在meteor应用中,如果所使用的开放平台账号和公众平台账号 已经绑定,那么登录应用后不仅可以获得openId,还可以获得unionId,此时,只要应用使用unionId作为服务微信服务的mainId,就可以打通两 种方式登录。

配置指南

注意,该配置仅供参考,表达原理,具体方式还需要根据应用实际情况确定。

  1. 应用使用的开放平台账号和公众平台账号要进行绑定
  2. meteor应用同时安装accounts-wechat和accounts-wechat-mp
  3. 配置服务时,mainId均使用unionId(而非openId)
  4. 同步meteor用户的两种服务的id字段,比如,若用户A绑定到wechat时,则设置A.services['wechat-mp'].id = A.services['wechat'].id。 再比如,若用户系统设计为用户只能使用微信登录,而没有其他方式变更用户绑定的微信,则可以
Accounts.onCreateUser(function (options, user) {
    if (options.profile) {
        user.profile = options.profile;
    }

    const wechatUnionId = _.get(user, 'services.wechat.id') || _.get(user, 'services.wechat-mp.id');
    if (wechatUnionId) {
        _.set(user, 'services.wechat.id', wechatUnionId);
        _.set(user, 'services.wechat-mp.id', wechatUnionId);
    }

    return user;
});

Note:

微信开放平台相关应用的授权回调域、对应Meteor应用的ROOT_URL以及用户访问该应用的实际url必须保持一致。

在绑定微信所有平台,需要先进行开发者认证,微信需要你有自己的服务器资源,填写的URL需要正确响应微信发送的Token验证,具体的js 在这里可以找到:如何接入微信公众平台(meteor)

为什么要安装zhaoyao91:short-oauth-state包?
因为Meteor默认将auth state编码到url中,导致url比较长,而微信对url的长度有限制,导致认证失败。该包仅提供一种workaround方案:将auth state保存到服务端,而仅在url中保存该state的id,从而缩短url长度。如果您有更好的解决方案,欢迎向社区做贡献!

参考

meteor-accounts-wechat-mp's People

Contributors

seanjsong avatar xyx2011 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.