Answer the question
In order to leave comments, you need to log in
How to get and write data from social networks using eauth yii2 during registration?
I did the registration on the tutorial .
It does not specify how to get data when entering from the user and write it to the database. Well, the creation of a new user.
Answer the question
In order to leave comments, you need to log in
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');
$attributes = $client->getUserAttributes();
$email = '';
$sex = 0;
// VK
if($client instanceof VKontakte)
{
$auth_params = $client->getAccessToken()->getParams();
$email = ArrayHelper::getValue($auth_params,'email','');
// Аватарка из соц сети
$vk_data_response = $client->api('users.get','POST',['uids'=> $attributes['id'] , 'fields'=> 'photo_max_orig']);
if($vk_data_response = ArrayHelper::getValue($vk_data_response,'response',false))
{
$vk_data = array_shift($vk_data_response);
Yii::$app->session->setFlash('social_avatar', $vk_data['photo_max_orig']);
}
}
if($client instanceof YandexOAuth)
{
$ya_data_response = $client->api('info','GET',['format'=>'json']);
$email = array_shift($ya_data_response['emails']);
Yii::$app->session->setFlash('social_avatar', "https://avatars.yandex.net/get-yapic/{$ya_data_response['default_avatar_id']}/islands");
if(!empty($ya_data_response['sex']))
{
$sex = $ya_data_response['sex'] == 'female' ? UserProfile::SEX_FEMALE : UserProfile::SEX_MALE;
}
}
if($client instanceof Odnoklassniki)
{
$od_data_response =$client->api('','GET',[
'method' => 'users.getCurrentUser',
'application_key' => $client->application_public_key
]);
$sex = $od_data_response['gender'] == 'female' ? UserProfile::SEX_FEMALE : UserProfile::SEX_MALE;
if(isset($od_data_response['pic_1']))
{
Yii::$app->session->setFlash('social_avatar', $od_data_response['pic_1']);
}
}
if($client instanceof Facebook) {
try
{
$curl = new Curl;
$fb_curl_result = $curl->get("https://graph.facebook.com/{$attributes['id']}/picture?width=300&redirect=false");
$fb_data_responce = Json::decode($fb_curl_result);
if (isset($fb_data_responce['data'])) {
Yii::$app->session->setFlash('social_avatar', $fb_data_responce['data']['url']);
}
}
catch (Exception $e) {};
}
$user_social = UserSocial::findOne([
'client' => $client_id ,
'internal_id' => $attributes['id']
]);
if(!$user_social)
{
$user = new User;
if(!empty($email) && !$user = User::find()->where(['email'=>$email])->one())
{
$user = new User;
$user->email = $email;
}
// тут я шаманил у меня соц пользователи не имеют логина и пароля через валидатор авторизации они не пройдут так как пустые.
$user->scenario = 'social';
if($user->isNewRecord)
{
$user->username = '';
$user->password_hash = '';
}
// регистрируем в User
if($user->register())
{
$user_social = new UserSocial;
$user_social->user_id = $user->id;
$user_social->client = $client_id;
$user_social->internal_id = (string)$attributes['id'];
$user_social->save();
// $this->referralRegistration($user->id);
}
//$this->setSocialAvatarToProfile($user->id);
Yii::$app->session->set('auth_social',[
'client' => $client_id,
'attributes' => $attributes,
'token' => ''
]);
}
$user = $user_social->user;
Yii::$app->user->login($user , $day * 366);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question