Yii2-Authclient

Hi all,

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']);

      }    

    }

  }    



However, as a result, the signup page loads in the popup used for logging in via the OAuth provider.

How should the successCallback be implemented in order for the popup to close, then redirecting the main browser window to the next page in my signup/ signin workflow?

I solved this problem by doing never open the login in a popup.

You can configure this in the widget:




 <?=

    yii\authclient\widgets\AuthChoice::widget([

        'popupMode' => false

    ]); ?>



Thx for your answer! I ended up using AuthAction’s redirect implementation. It cares for closing the popUp first. Seems a bit hacky, but works as expected. As a side note: AuthAction::redirect doesn’t like an array as input. Might be a Bug. So currently, I’m using code like that:




public function actions()

  {

    return [

      'auth' => [

        'class' => 'yii\authclient\AuthAction',

        'successCallback' => [$this, 'successCallback'],

      ],

    ];

  }


  /**

   * @param \yii\authclient\ClientInterface $client

   */

  public function successCallback($client)

  {

    if (!$this->action instanceof \yii\authclient\AuthAction) {

      throw new \yii\base\InvalidCallException("successCallback is only meant to be executed by AuthAction!");

    }


    $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->action->redirect( Url::toRoute(['private/index'],true) );

      }

      else

      {

        Yii::$app->session->set( 'signup/authProvider', $externalUser->authProvider );

        Yii::$app->session->set( 'signup/attributes'  , $attributes );

        

        return $this->action->redirect( Url::toRoute(['site/signup'],true) );

      }    

    }

  }