Coder Social home page Coder Social logo

passport-wechat-public's Introduction

npm version Dependencies Status Build Status Coverage Status Code Climate

passport-wechat-public

Passport strategy for authenticating with Wechat Official Accounts

Documents (English)

Passport的微信公众号OAuth2.0用户验证模块。支持的框架有Express,Strongloop/Loopback,支持Loopback-Component-Passport. (网页登录可以支持,但是没有测试过,因为没有相应的网站应用,微信也没有给相应的可以测试的测试号。但是功能比较一致,只是改变了authURL,所以加了,正式使用的话慎重测试)

微信企业号, 转至passport-wechat-enterprise

Install

$ npm install passport-wechat-public

Usage

Configure Strategy

在Passport注册WechatPublicStrategy, Passport.use()的第一个参数是name,可以忽略使用默认的名字’wechat-public'。WechatPublicStrategy的构造函数的参数是options和verify。 options的appId,appSecret和callbackURL是必须的,其他为可选。verify函数是验证或创建用户传给done函数

passport.use("wechat",new WechatPublicStrategy({
    appId: APP_ID,
    appSecret: APP_SECRET,
    callbackURL: "http://localhost:3000/auth/wechat/callback",
    state: "state",
    scope: "snsapi_base",
    agent: "wechat",
    getToken: function(openid, cb) {...cb(null, accessToken)}
    saveToken: function (openid, token, cb) {... /*save to db*/ cb(null)}
  },
  function(accessToken, refreshToken, profile, done) {
    User.findOrCreate({ openId: profile.id }, function (err, user) {
      return done(err, user);
    });
  }
));

getTokensaveToken 用于保存从微信服务器获得的用户的access token, 以免重复请求token,token的有效时间为2小时,如果忽略此方法,在production环境下console中会显示'Please dont save oauth token into memory under production'

Authenticate Requests

passport.authenticate()在对应的route下,注意strategy名字和passport.use()时一致。

For example

app.get('/auth/wechat',
  passport.authenticate('wechat'));

app.get('/auth/wechat/callback',
  passport.authenticate('wechat', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });

Loopback-Component-Passport

Loopback-Component-Passport 官方文档

providers.json加入wechat provider即可,profile的id就是openid

{
  "wechat": {
    "provider": "wechat",
    "module": "passport-wechat-public",
    "callbackURL": "/auth/wechat/callback",
    "successRedirect": "/auth/wechat/account",
    "failureRedirect": "/auth/wechat/failure",
    "scope": ["snsapi_userinfo"],
    "appId": "wxabe757c89bb6d74b",
    "appSecret": "9a62bc24a31d5c7c2b1d053515d276f8"
  }
}
  • providers.json 不能直接写方法,需要在configureProvider 时将getAccessToken saveAccessToken 加入options中
function getAccessToken(cb) {...};
function saveAccessToken(accessToken, cb){...};
for (var s in config) {
    var c = config[s];
    c.session = c.session !== false;
    if(s === 'wechat') {
      c.getToken = getAccessToken;
      c.saveToken = saveAccessToken;
    }
    passportConfigurator.configureProvider(s, c);
  }

passport-wechat-public's People

Contributors

vcwen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

kesyn fregatte123

passport-wechat-public's Issues

认证完之后的重定向问题

我有一个url http://127.0.0.1:3000/event/:id, 这个网页下的内容,需要获得微信网页授权。

我的基本思路是,检测当前User是否有授权,没有的话就 GET http://127.0.0.1:3000/event/:id/auth/wechat, 获得授权之后,再重定向到 'http://127.0.0.1:3000/event/:id'。刚开始学web, 不知道思路对不对。

这是我在providers.json里关于微信login的配置。

  "wechat-login": {
    "provider": "wechat",
    "module": "passport-wechat-public",
    "appId": "wxdfsafadsfasfdfa",
    "appSecret": "b79a000339b2fdsfafdsafsfa",
    "callbackURL": "http://127.0.0.1:3000/auth/wechat/callback",
    "callbackPath": "/auth/wechat/callback",
    "authPath": "/auth/wechat",
    "scope": ["snsapi_base"],
    "agent": "wechat",
    "authScheme": "OAuth 2.0",
    "state": "login"
  }

server.js 里面加了这么一段代码

app.get('/event/:id/auth/wechat', passport.authenticate('wechat-login'));

app.get('/event/:id/auth/wechat/callback',
  passport.authenticate('wechat-login', {
    successRedirect: '/event/123',
    failureRedirect: '/login'
  })
);

我在微信Web开发者工具里输入 http://127.0.0.1:3000/event/123/auth/wechat,授权成功之后还是定向到 /account. 请问这个需要怎么写?

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.