And so first we need to create an application for authorization using Facebook, here:

Maybe creating an application and configure it on Facebook should be step 1 but since this step is not described here just know that it must be completed in order to log in via Facebook to the site.

Step 1. Install yii2-authclient

Next, open the console, go to the folder with the project and install yii2-authclient using composer.

composer require --prefer-dist yiisoft/yii2-authclient

Step 2. Connect and configure yii2-authclient

Set up yii2-authclient, open the config/main.php settings file and add code to components:

'authClientCollection' => [
    'class' => 'yii\authclient\Collection',
    'clients' => [
        'facebook' => [
            'class' => 'yii\authclient\clients\Facebook',
            //'authUrl' => '',
            'clientId' => 'APP_CLIENT_ID',
            'clientSecret' => 'APP_CLIENT_SECRET',
            'attributeNames' => ['name', 'email', 'first_name', 'last_name'],

In attributeNames, you can pass the field name as a parameter that you want to get from Facebook. Parameters that can be passed to get more information about the user here:

Step 3. Configure the controller

In SiteController, add an interaction between yii2-authclient and the controller. We add the following code to the actions method:

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

In successCallback we give the name of the action that will be called upon successful authentication. In this example, this is oAuthSuccess.

Step 4. Add the oAuthSuccess method

public function oAuthSuccess($client) {
    $userAttributes = $client->getUserAttributes();
    echo $userAttributes['email'];

Here we make registration, authorization or what you need on the site to enter.

Step 5. Display the button of login with Facebook

To display the button, use following code:

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

Now we can test everything and switch the application (here: from test to public status.