yii2facebook-loginyii2-advanced-appyii2-authclient

Eerror in social login in Yii2: Setting unknown property: yii\web\Application::authClientCollection


When I try to access app it get error after use yii2-authclient. I follow Facebook Authentication using Yii2 authclient

All is setup, but this error occur:

Setting unknown property: yii\web\Application::authClientCollection

this is error detail which is i faced during execution my yii app

My frontend/main.php is:

'components' => [
        'request' => [
            'csrfParam' => '_csrf-frontend',
        ],
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
        ],
        'session' => [
            // this is the name of the session cookie used for login on the frontend
            'name' => 'advanced-frontend',
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
//        'urlManager' => [
//            'enablePrettyUrl' => true,
//            'showScriptName' => false,
//            'rules' => [
//            ],
//        ],
    ],
    'authClientCollection' => [
        'class' => 'yii\authclient\Collection',
        'clients' => [
            'facebook' => [
                'class' => 'yii\authclient\clients\Facebook',
                'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup',
                'clientId' => '2061735130522390',
                'clientSecret' => '2f302dc7358730820c091ca4444afbae',
                'attributeNames' => ['name', 'email', 'first_name', 'last_name'],
            ],
        ],
    ],

My site controller is:

class SiteController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public $successUrl = "success";
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout', 'signup'],
                'rules' => [
                    [
                        'actions' => ['signup'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
            'auth' => [
                'class' => 'yii\authclient\AuthAction',
                'successCallback' => [$this, 'successCallback'],
            ],
        ];
    }

    public function successCallback($client) {
        // get user data from client
        $userAttributes = $client->getUserAttributes();

        $user = User::find()->where(['email'=>$userAttributes['email']])->one();
        if (!empty($user))
        {
            Yii::$app->user->login($user);
        }
        else{
            $session = Yii::$app->session;
            $session['attribute'] = $userAttributes;
            $this->successUrl = Url::to(['signup']);
        }
        die(print_r($userAttributes));
        // do some thing with user data. for example with $userAttributes['email']
    }
    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * Logs in a user.
     *
     * @return mixed
     */
.
.
.

}

login.php is:

<div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin(['id' => 'login-form']); ?>

                <?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>

                <?= $form->field($model, 'password')->passwordInput() ?>

                <?= $form->field($model, 'rememberMe')->checkbox() ?>

                <div style="color:#999;margin:1em 0">
                    If you forgot your password you can <?= Html::a('reset it', ['site/request-password-reset']) ?>.
                </div>

                <div class="form-group">
                    <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
                </div>
            <p>OR</p>
            <?= yii\authclient\widgets\AuthChoice::widget([
                'baseAuthUrl' => ['site/auth']
            ]) ?>

            <?php ActiveForm::end(); ?>
        </div>
    </div>

Solution

  • Your config is incorrect, authClientCollection config should be inside of components array - in your case it is outside of it. You should move authClientCollection element one line up and change this:

    ],
    'authClientCollection' => [
        'class' => 'yii\authclient\Collection',
        'clients' => [
            'facebook' => [
                'class' => 'yii\authclient\clients\Facebook',
                'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup',
                'clientId' => '2061735130522390',
                'clientSecret' => '2f302dc7358730820c091ca4444afbae',
                'attributeNames' => ['name', 'email', 'first_name', 'last_name'],
            ],
        ],
    ],
    

    To this:

        'authClientCollection' => [
            'class' => 'yii\authclient\Collection',
            'clients' => [
                'facebook' => [
                    'class' => 'yii\authclient\clients\Facebook',
                    'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup',
                    'clientId' => '2061735130522390',
                    'clientSecret' => '2f302dc7358730820c091ca4444afbae',
                    'attributeNames' => ['name', 'email', 'first_name', 'last_name'],
                ],
            ],
        ],
    ],