yuyangame/yii2-oauth

yii2-oauth

安装次数: 29

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:yii2-extension

0.1.1 2015-06-16 08:45 UTC

This package is not auto-updated.

Last update: 2024-10-02 10:30:15 UTC


README

yii2-oauth 包含:QQ、微博、微信、人人、豆瓣

#安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require --prefer-dist yuyangame/yii2-oauth "*"

或者将以下内容添加到您的 composer.json 文件的 require 部分:

"yuyangame/yii2-oauth": "*"

to the require section of your composer.json file.

#使用

扩展安装完成后,只需在代码中按以下步骤使用即可

1. 添加组件配置

'components' => [
    'authClientCollection' => [
        'class' => 'yii\authclient\Collection',
        'clients' => [
            'qq' => [
                'class' => 'yuyangame\oauth\QqAuth',
                'clientId' => 'YOUR_OPEN_APPID',
                'clientSecret' => 'YOUR_OPEN_APPSCRET',
            ],
            'weibo' => [
                'class' => 'yuyangame\oauth\WeiboAuth',
                'clientId' => 'YOUR_OPEN_APPID',
                'clientSecret' => 'YOUR_OPEN_APPSCRET',
            ],
            'weixin' => [
                'class' => 'yuyangame\oauth\WeixinAuth',
                'clientId' => 'YOUR_OPEN_APPID',
                'clientSecret' => 'YOUR_OPEN_APPSCRET',
            ],
        ]
    ]
    ...
]

2. 添加控制器

class SiteController extends Controller
{
    public function actions()
    {
        return [
            'auth' => [
                'class' => 'yii\authclient\AuthAction',
                'successCallback' => [$this, 'successCallback'],
            ],
        ];
    }

    /**
     * Success Callback
     * @param QqAuth|WeiboAuth $client
     * @see http://wiki.connect.qq.com/get_user_info
     * @see http://stuff.cebe.cc/yii2docs/yii-authclient-authaction.html
     */
    public function successCallback($client) {
        $id = $client->getId(); // qq | weibo | weixin
        $attributes = $client->getUserAttributes(); // basic info
        $userInfo = $client->getUserInfo(); // user extend info
        // login or signup
    }
}

3. 添加视图

<?=
yii\authclient\widgets\AuthChoice::widget([
    'baseAuthUrl' => ['site/auth'],
    //'popupMode' => false,
])
?>