Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AuthAction in yii2

Tags:

oauth

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?

like image 906
Dinesh Rawat Avatar asked Oct 21 '15 06:10

Dinesh Rawat


2 Answers

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;
}
like image 77
keeg Avatar answered Oct 22 '22 02:10

keeg


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!

like image 35
ThanhPV Avatar answered Oct 22 '22 02:10

ThanhPV