I’m developing an integration of my application with Twitter and Facebook. At the same time I want to get Facebook and Twitter connect the application, but did not have an idea of how to implement the two together.
Individually the oAuth works but I need to click the button to work.
How Automatically validate the connections?
frontend/config/main.php
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup',
'clientId' => '1639200353072063',
'clientSecret' => '607755f3156df0b27ac3d9ef65f7c18a',
'scope' => [
'email',
'public_profile',
'user_about_me',
'user_location',
'user_work_history',
]
],
'twitter' => [
'class' => 'yii\authclient\clients\Twitter',
'consumerKey' => 'qKRQ5fYwGyhiptlYuQYv0bTNf',
'consumerSecret' => 'rf565VH49XYCM6VqlTkadJyQfMJj0Rx2Y91MvFwGrE64Ayp1x8',
],
],
],
SocialNetworkController
<?php
namespace frontend\controllers;
use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\AccessControl;
use yii\helpers\Url;
class SocialNetworkController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
]
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'auth' => [
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'oAuthSuccess'],
'successUrl' => Url::to(['social-network/index'])
],
];
}
public function actionIndex()
{
return $this->render('index');
}
public function oAuthSuccess($client) {
// Método para tratar o oAuth client. Registrar, salvar dados, etc...
}
}
view/index.php
<?php
use yii\helpers\Html;
use yii\helpers\Url;
use yii\authclient\widgets\AuthChoice;
$this->title = Yii::t('app', 'Social Network');
$this->params['breadcrumbs'][] = ['label' => $this->title];
$authAuthChoice = AuthChoice::begin([
'baseAuthUrl' => ['social-network/auth'],
'autoRender' => false,
'options' => ['class' => '']
]);
?>
<?php foreach ($authAuthChoice->getClients() as $client): ?>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-<?=$client->getName()?>" aria-hidden="true"></i> <?=$client->getTitle()?></h3>
</div>
<div class="panel-body">
<?php
if(is_null($client->getUserAttributes())){
echo Html::a(Yii::t('app', 'Log in with'). $client->title, ['social-network/auth', 'authclient'=> $client->name, ], ['class' => "btn btn-block btn-default $client->name "]);
} else {
echo Yii::t('app', 'Logged in as'). " " . $client->getUserAttributes()['name'];
}
?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
<?php AuthChoice::end();?>
<?php
// $this->registerJs("
// $.ajax({
// url: '".Url::to(['social-network/auth'])."',
// data: {'authclient' : 'facebook'},
// type: 'get',
// success: function(data) {
// alert('oi');
// },
// });
// ");
?>
PS: I do not want to log in my application, I already have a user module. I only want to associate the two social networks on my application.