Answer the question
In order to leave comments, you need to log in
Nodge - Yii2 EAuth - registration, Identity, how are you doing?
I use an extension for Yii2, social authorization. by Nodge
I have everything set up and everything works, but I want to do it right, but due to my little experience I can't come up with an elegant solution yet. Authorization works properly, I wrote my own registration.
Essence of the question:
Actually, I didn’t change much of the author’s code, I only expanded the receipt of attributes from Api-shek. The difficulty is that I get two Identity
Standard views: __id: 3
Social views: __id: vkontakte-484848
There is a code out of the box for Yii2 and you can pull out or show the profile for the current authorized user in this way. user and profile accounts are separated in the database, but linked one to one. those. their Ids are the same. I can get the profile like this:
$modelProfile = Profile::find()->where(['id' => Yii::$app->user->identity->id])->one();
protected function getSocialUser()
{
$identity = Yii::$app->getUser()->getIdentity();
$user = User::find()->where(['service_id' => $identity->profile['id']])->one();
$profile = Profile::find()->where(['id' => $user->id])->one();
return $profile;
}
$model = $this->getSocialUser();
if(!isset($model)){
$model = $this->findModel(Yii::$app->user->identity->id);
}
Answer the question
In order to leave comments, you need to log in
i made a separate table for social authorization
user - created an entry here
+
user_social - created an entry here if using authorization through the social network
vendor str (vk fb ya mr tw etc.)
user_id int
internal_id str
The user_social table was used only when logging in or registering from social networks. networks
If the required data is from social networks, I created campaign fields in the user or user_profile tables
UPD
public function actions()
{
return [
'social' => [
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'successCallback'],
],
];
}
/**
* @param $client
*/
public function successCallback($client)
{
$day = 86400;
$client_id = Yii::$app->request->get('authclient'); // vk fb mr od etc
$attributes = $client->getUserAttributes(); // result social data
$userSocial = UserSocial::findOne(['client' => $client_id , 'internal_id' => $attributes['id'] ]);
if(!$userSocial ){
$user = new User;
$user->scenario = 'social';
if($user->register()){
$userSocial = new UserSocial;
$userSocial ->user_id = $user->id;
$userSocial ->client = $client_id;
$userSocial ->internal_id = (string)$attributes['id'];
$userSocial ->save();
$prorile = $user->profile;
$prorile->first_name = $attributes['first_name'];
$prorile->last_name = $attributes['last_name'];
$prorile->save();
}
Yii::$app->user->login($user, $day * 366 );
Yii::$app->session->set('auth_social',[
'client' => $client_id,
'attributes' => $attributes,
'token' => ''
]);
}
Yii::$app->user->login($user_social->user , $day * 366 );
}
<?php
use yii\authclient\widgets\AuthChoice;
$icons_id = [
'vkontakte' => 1,
'facebook' => 2,
'tw' => 3,
'mr' => 4,
'odnoklassniki' => 5
];
$authAuthChoice = AuthChoice::begin([
'baseAuthUrl' => ['auth/social'],
]);?>
<ul class="social-auth-list">
<?php foreach ($authAuthChoice->getClients() as $client): ?>
<li>
<a data-popup-width="500" data-popup-height="430"
href="<?= $authAuthChoice->createClientUrl($client)?>"><i class="ico ico-soc-<?=$icons_id[$client->getId()]?>"></i></a>
</li>
<?php endforeach; ?>
</ul>
<?php AuthChoice::end(); ?>
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'vkontakte' => [
'class' => 'yii\authclient\clients\VKontakte',
'clientId' => '1',
'clientSecret' => 'vW5AYsdsd',
'scope' => 'friends,email,offline',
],
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'clientId' => '1',
'clientSecret' => 'vW5AYsdsd',
],
'odnoklassniki' => [
'class' => 'app\components\authclient\clients\Odnoklassniki',
'clientId' => '1',
'clientSecret' => 'F12sdsd',
'application_public_key' => 'vW5AYsdsdsdsd',
'scope' => 'VALUABLE_ACCESS'
],
],
],
<?php
namespace app\components\authclient\clients;
use yii\authclient\OAuth2;
/**
* Class Odnoklassniki
* @package app\components\authclient\clients
* Example application configuration:
*
* ~~~
* 'components' => [
* 'authClientCollection' => [
* 'class' => 'yii\authclient\Collection',
* 'clients' => [
* 'odnoklassniki' => [
* 'class' => 'yii\authclient\clients\odnoklassniki',
* 'clientId' => 'app_client_id',
* 'clientSecret' => 'application_client_secret',
* 'application_public_key' => 'application_public_key',
* 'scope' => 'VALUABLE_ACCESS'
* ],
* ],
* ]
* ...
* ]
*/
class Odnoklassniki extends OAuth2
{
/**
* @inheritdoc
*/
public $authUrl = 'http://www.odnoklassniki.ru/oauth/authorize';
/**
* @inheritdoc
*/
public $tokenUrl = 'http://api.odnoklassniki.ru/oauth/token.do';
/**
* @inheritdoc
*/
public $apiBaseUrl = 'http://api.odnoklassniki.ru/fb.do';
public $application_public_key;
/**
* @inheritdoc
*/
protected function initUserAttributes()
{
return $this->api('','GET',[
'method' => 'users.getCurrentUser',
'format' => 'JSON',
'application_key' => $this->application_public_key,
'client_id' => $this->clientId,
]);
}
/**
* @inheritdoc
*/
protected function apiInternal($accessToken, $url, $method, array $params, array $headers)
{
$access_token = $accessToken->getToken();
if (count($params)) {
$param_str = '';
ksort($params);
foreach ($params as $k => $v)
{
$param_str .= $k . '=' . $v;
}
$params['sig'] = md5($param_str . md5($access_token . $this->clientSecret));
}
$params['access_token'] = $access_token;
return $this->sendRequest($method, trim($url,'/'), $params, $headers);
}
/**
* @inheritdoc
*/
protected function defaultName()
{
return 'odnoklassniki';
}
/**
* @inheritdoc
*/
protected function defaultTitle()
{
return 'Odnoklassniki';
}
/**
* @inheritdoc
*/
protected function defaultNormalizeUserAttributeMap()
{
return [
'id' => 'uid'
];
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question