Skip to content

Latest commit

 

History

History
205 lines (163 loc) · 6.04 KB

social-auth.md

File metadata and controls

205 lines (163 loc) · 6.04 KB

Authentication via social networks

Yii2-user provides user registration and login using social sites credentials. It also allows to connect multiple social networks to user account and use them to log in.

Getting started

To get started you should configure authClientCollection application component:

...
'components' => [
    ...
    'authClientCollection' => [
        'class'   => \yii\authclient\Collection::className(),
        'clients' => [
            // here is the list of clients you want to use
            // you can read more in the "Available clients" section
        ],
    ],
    ...
],
...

Available clients

Here is the list of clients supported by the module:

Facebook

  • You can register new application and get secret keys here.
'facebook' => [
    'class'        => 'dektrium\user\clients\Facebook',
    'clientId'     => 'APP_ID',
    'clientSecret' => 'APP_SECRET',
],

Twitter

  • You can register new application and get secret keys here.

NOTE: Current version of Twitter API does not provide user's email address whithout elevated permissions (https://dev.twitter.com/rest/reference/get/account/verify_credentials), so we can't register user without making him enter his email address

'twitter' => [
    'class'          => 'dektrium\user\clients\Twitter',
    'consumerKey'    => 'CONSUMER_KEY',
    'consumerSecret' => 'CONSUMER_SECRET',
],
  • If you have requested elevated permissions for your app, you need to send the include_email parameter to obtain the user email
'twitter' => [
    'class'          => 'dektrium\user\clients\Twitter',
    'consumerKey'    => 'CONSUMER_KEY',
    'consumerSecret' => 'CONSUMER_SECRET',
    'attributeParams' => [
        'include_email' => 'true'
    ],
],

Google

  • You can register new application and get secret keys here.
  • First of all you need to enable Google+ API in APIs & auth section.
  • Then you need to create new client id on APIs & auth > Credentials section
  • Authorized JavaScript origins should contain url like http://localhost
  • Authorized redirect URIs should contain url like http://localhost/user/security/auth?authclient=google
'google' => [
    'class'        => 'dektrium\user\clients\Google',
    'clientId'     => 'CLIENT_ID',
    'clientSecret' => 'CLIENT_SECRET',
],

Github

  • You can register new application and get secret keys here.
'github' => [
    'class'        => 'dektrium\user\clients\GitHub',
    'clientId'     => 'CLIENT_ID',
    'clientSecret' => 'CLIENT_SECRET',
],

VKontakte

  • You can register new application and get secret keys here.
'vkontakte' => [
    'class'        => 'dektrium\user\clients\VKontakte',
    'clientId'     => 'CLIENT_ID',
    'clientSecret' => 'CLIENT_SECRET',
]

Yandex

  • You can register new application and get secret keys here.
  • Make sure that you have enabled access to email address in Yandex.Passport API section.
  • Also you should set the callback url to url like http://localhost/user/security/auth?authclient=yandex.
'yandex' => [
    'class'        => 'dektrium\user\clients\Yandex',
    'clientId'     => 'CLIENT_ID',
    'clientSecret' => 'CLIENT_SECRET'
],

LinkedIn

  • You can register new application and get secret keys here
'linkedin' => [
    'class'        => 'dektrium\user\clients\LinkedIn',
    'clientId'     => 'CLIENT_ID',
    'clientSecret' => 'CLIENT_SECRET'
],

Other networks

Yii2-user also supports all social networks provied by Yii2-authclient extension. However it does not support registration without entering email and username after authenticating via network.

Configuration example

The following config allows to log in using 3 networks (Twitter, Facebook and Google):

'authClientCollection' => [
    'class' => yii\authclient\Collection::className(),
    'clients' => [
        'facebook' => [
            'class'        => 'dektrium\user\clients\Facebook',
            'clientId'     => 'CLIENT_ID',
            'clientSecret' => 'CLIENT_SECRET',
        ],
        'twitter' => [
            'class'          => 'dektrium\user\clients\Twitter',
            'consumerKey'    => 'CONSUMER_KEY',
            'consumerSecret' => 'CONSUMER_SECRET',
        ],
        'google' => [
            'class'        => 'dektrium\user\clients\Google',
            'clientId'     => 'CLIENT_ID',
            'clientSecret' => 'CLIENT_SECRET',
        ],
    ],
],

How to access social network attributes

You can fill user's profile with data provided by social network, here is quick example of how to fill profile name with the name provided via facebook:

// plase this code somewhere in your config files (bootstrap.php in case of using advanced app template, web.php in case
// of using basic app template

use dektrium\user\controllers\SecurityController; 

Event::on(SecurityController::class, SecurityController::EVENT_AFTER_AUTHENTICATE, function (AuthEvent $e) {
    // if user account was not created we should not continue
    if ($e->account->user === null) {
        return;
    }

    // we are using switch here, because all networks provide different sets of data
    switch ($e->client->getName()) {
        case 'facebook':
            $e->account->user->profile->updateAttributes([
                'name' => $e->client->getUserAttributes()['name'],
            ]);
        case 'vkontakte':
            // some different logic
    }
    
    // after saving all user attributes will be stored under account model
    // Yii::$app->identity->user->accounts['facebook']->decodedData
});