yii2 Add auth action and set up callback


Example

  1. Add button Login as facebook account to your login view:

Edit site/login.php in views folder, add theses line to content of page login:

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

Above, we set that auth action in SiteController will handler OAuth2 flow.

Now we create it.

class SiteController extends Controller
{
    public function actions()
    {
        return [
            'auth' => [
                'class' => 'yii\authclient\AuthAction',
                'successCallback' => [$this, 'onAuthSuccess'],
            ],
        ];
    }

    public function onAuthSuccess($client)
    {
        // do many stuff here, save user info to your app database
    }
}

We use yii\authclient\AuthAction for create url and redirect to facebook login page.

Function onAuthSuccess used to get user info, login to your app.