Dinesh Rawat
Dinesh Rawat

Reputation: 1019

AuthAction in yii2

I'm trying to implement yii\authclient\AuthAction's successCallback.

My code looks like this:

public function actions()
  {
    return [
      'auth' => [
        'class' => 'yii\authclient\AuthAction',
        'successCallback' => [$this, 'successCallback'],
      ],
    ];
  }

  /**
   * @param \yii\authclient\ClientInterface $client
   */
  public function successCallback($client)
  {
    $attributes = $client->getUserAttributes();

    $externalUser = new AuthForm();
    $externalUser->authProvider = $client->getName();
    $externalUser->externalUserId = array_key_exists('id', $attributes) ? $attributes['id'] : null;

    if ($externalUser->validate())
    {
      if ($externalUser->isRegistered())
      {
        $externalUser->login();
        return $this->redirect(['private/index']);
      }
      else
      {
        Yii::$app->session->set( 'signup/authProvider', $externalUser->authProvider );
        Yii::$app->session->set( 'signup/attributes'  , $attributes );

        return $this->redirect(['site/signup']);
      }    
    }
  } 

How can I call successCallback? I want to call the auth method. But I am not able to do this?

Upvotes: 9

Views: 1214

Answers (2)

ThanhPV
ThanhPV

Reputation: 463

It will run successCallback when Auth server response successful. You must config authcollection (collection config of auth server)

'components' => [
    'authClientCollection' => [
        'class' => 'yii\authclient\Collection',
        'clients' => [
            'google' => [
                'class' => 'yii\authclient\clients\GoogleOpenId'
            ],
            'facebook' => [
                'class' => 'yii\authclient\clients\Facebook',
                'clientId' => 'facebook_client_id',
                'clientSecret' => 'facebook_client_secret',
            ],
            // etc.
        ],
    ]
    ...
]

Default: Yii2 authclient support some openid, oauth, oauth2 provider:

  • [[\yii\authclient\clients\Facebook|Facebook]].
  • [[yii\authclient\clients\GitHub|GitHub]].
  • Google (via [[yii\authclient\clients\GoogleOpenId|OpenID]] and [[yii\authclient\clients\GoogleOAuth|OAuth]]).
  • [[yii\authclient\clients\LinkedIn|LinkedIn]].
  • [[yii\authclient\clients\Live|Microsoft Live]].
  • [[yii\authclient\clients\Twitter|Twitter]].
  • [[yii\authclient\clients\VKontakte|VKontakte]].
  • Yandex (via [[yii\authclient\clients\YandexOpenId|OpenID]] and [[yii\authclient\clients\YandexOAuth|OAuth]]).

There's ready to use [[yii\authclient\widgets\AuthChoice]] widget to use in views:

<?= yii\authclient\widgets\AuthChoice::widget([
     'baseAuthUrl' => ['site/auth'],
     'popupMode' => false,
]) ?>

For more infomation: https://github.com/yiisoft/yii2-authclient/tree/master/docs/guide

Goodluck and have fun!

Upvotes: 1

keeg
keeg

Reputation: 3978

Most likely this is working fine, but you did not permit for the action of auth to be accessed. Make sure you allow auth in your behaviours of your controller. Something like:

public function behaviors() {
    $behaviors = parent::behaviors();

    $behaviors [ 'access' ] = [
        'rules' => [
            [
                'actions' => [ 'auth' ],
                'allow'   => true,
            ],
        ],
    ];

    return $behaviors;
}

Upvotes: 1

Related Questions