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.
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
],
],
...
],
...
Here is the list of clients supported by the module:
- You can register new application and get secret keys here.
'facebook' => [
'class' => 'dektrium\user\clients\Facebook',
'clientId' => 'APP_ID',
'clientSecret' => 'APP_SECRET',
],
- 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'
],
],
- 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 likehttp://localhost
Authorized redirect URIs
should contain url likehttp://localhost/user/security/auth?authclient=google
'google' => [
'class' => 'dektrium\user\clients\Google',
'clientId' => 'CLIENT_ID',
'clientSecret' => 'CLIENT_SECRET',
],
- You can register new application and get secret keys here.
'github' => [
'class' => 'dektrium\user\clients\GitHub',
'clientId' => 'CLIENT_ID',
'clientSecret' => 'CLIENT_SECRET',
],
- You can register new application and get secret keys here.
'vkontakte' => [
'class' => 'dektrium\user\clients\VKontakte',
'clientId' => 'CLIENT_ID',
'clientSecret' => 'CLIENT_SECRET',
]
- 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'
],
- You can register new application and get secret keys here
'linkedin' => [
'class' => 'dektrium\user\clients\LinkedIn',
'clientId' => 'CLIENT_ID',
'clientSecret' => 'CLIENT_SECRET'
],
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.
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',
],
],
],
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
});