README.md 1.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
AuthClient Extension for Yii 2
==============================

This extension adds [OpenID](http://openid.net/), [OAuth](http://oauth.net/) and [OAuth2](http://oauth.net/2/) consumers for the Yii 2 framework.


Installation
------------

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require yiisoft/yii2-authclient "*"
```

or add

```json
"yiisoft/yii2-authclient": "*"
```

Qiang Xue committed
24
to the `require` section of your composer.json.
25 26 27 28 29


Usage & Documentation
---------------------

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
This extension provides the ability of the authentication via external credentials providers.
It covers OpenID, OAuth1 and OAuth2 protocols.

You need to setup auth client collection application component:

```
'components' => [
    'authClientCollection' => [
        'class' => 'yii\authclient\Collection',
        'clients' => [
            'google' => [
                'class' => 'yii\authclient\clients\GoogleOpenId'
            ],
            'facebook' => [
                'class' => 'yii\authclient\clients\Facebook',
                'clientId' => 'facebook_client_id',
                'clientSecret' => 'facebook_client_secret',
            ],
        ],
    ]
    ...
]
```

Qiang Xue committed
54
Then you need to add [[yii\authclient\AuthAction]] to some of your web controllers:
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

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

    public function successCallback($client)
    {
Qiang Xue committed
71
        $attributes = $client->getUserAttributes();
72 73 74 75 76 77 78 79
        // user login or signup comes here
    }
}
```

You may use [[yii\authclient\widgets\Choice]] to compose auth client selection:

```
Antonio Ramirez committed
80
<?= yii\authclient\widgets\Choice::widget([
81
     'baseAuthUrl' => ['site/auth']
Qiang Xue committed
82 83
]) ?>
```