2017 © Pedro Peláez
 

library passport-wechat-oauth-grant

Wechat Oauth Grant For Passport

image

fmwww/passport-wechat-oauth-grant

Wechat Oauth Grant For Passport

  • Tuesday, April 3, 2018
  • by fmwww
  • Repository
  • 1 Watchers
  • 5 Stars
  • 38 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 41 % Grown

The README.md

passport-wechat-oauth-grant

增加一个可以通过微信Oauth授权code登录passport的Grant, (*1)

安装

通过Composer安装, (*2)

$ composer require fmwww/passport-wechat-oauth-grant

如果你的Laravel版本 < 5.5, 你需要在 config/app.php 的providers数组中增加:, (*3)

Fmwww\PassportWechatOauthGrant\WechatOauthGrantServiceProvider::class,

配置

使用之前需要先配置好微信的 app_idapp_secret 使用下面的命令拷贝配置文件到 config :, (*4)

$ php artisan vendor:publish --provider="Fmwww\PassportWechatOauthGrant\WechatOauthGrantServiceProvider"
return [
    /*
     * 微信app_id
     */
    'app_id' => env('WECHAT_OAUTH_GRANT_APP_ID', ''),
    /*
     * 微信app_secret
     */
    'app_secret' => env('WECHAT_OAUTH_GRANT_APP_SECRET', ''),
];

推荐使用 .env 文件进行配置, (*5)

用法

  • 使用POST方法去请求https://your-site.com/oauth/token
  • POST请求体里的需要将grant_type设置为wechat_oauth,同时将code设置为微信返回的授权code
  • 系统将会根据 config/auth.php里面的 api guard 设置的用户模型去寻找用户,如果用户模型定义了 findForWechatOauth 方法,那么就会使用这个方法返回的用户进行认证,否则就会根据openid字段去寻找用户进行认证。
  • 如果用户存在,就会成功返回 access_tokenrefresh_token

例子

请求方法

$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'wechat_oauth',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'code' => '001qJKS42a3r3N0wrDU42IHrS42qJKSN', #微信的授权code
        'scope' => '',
    ],
]);

return json_decode((string) $response->getBody(), true);

自定义 findForWechatOauth方法

public function findForWechatOauth($tokens)
    {
        // 获取openid
        $openid = $tokens->openid;

        // 获取access_token
        $access_token = $tokens->access_token;

        // 获取expires_in
        $expires_in = $tokens->expires_in;

        // 获取refresh_token
        $refresh_token = $tokens->refresh_token;

        // 获取scope
        $scope = $tokens->scope;

        // 通过openid查找,如果这个方法没有定义,默认就是这样查找
        $user = $this->where('openid', $openid)->first();

        // 你也可以自己创建用户然后返回
        if (empty($user)) {
            $this->openid = $openid;
            $this->save();
        }
        // 返回用户
        return $user ?: $this;
    }

The Versions

03/04 2018

dev-master

9999999-dev

Wechat Oauth Grant For Passport

  Sources   Download

The Requires

 

by FMW

03/04 2018

v1.0.4

1.0.4.0

Wechat Oauth Grant For Passport

  Sources   Download

The Requires

 

by FMW

03/04 2018

v1.0.3

1.0.3.0

Wechat Oauth Grant For Passport

  Sources   Download

The Requires

 

by FMW

03/04 2018

v1.0.2

1.0.2.0

Wechat Oauth Grant For Passport

  Sources   Download

The Requires

 

by FMW

03/04 2018

v1.0.1

1.0.1.0

Wechat Oauth Grant For Passport

  Sources   Download

The Requires

 

by FMW

25/03 2018

v1.0

1.0.0.0

Wechat Oauth Grant For Passport

  Sources   Download

The Requires

 

by FMW