我正在尝试实现yii\authclient\AuthAction的successCallback。

我的代码如下所示:

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

我怎么称呼successCallback?我想调用auth方法。但是我不能这样做吗?

最佳答案

这很可能运行良好,但是您不允许访问auth的操作。确保在 Controller 的行为中允许auth。就像是:

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

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

    return $behaviors;
}

10-05 22:53