Gvep
Gvep

Reputation: 1206

Yii 2 - authclient doesn't call onAuthSuccess

I am using Yii authclient to use social login. I had set everything as it is defined in docs but when I try to login with google it does not call onAuthSuccess method. When I try to login it just redirects me to returnUrl but not authenticated. Here is my code;

config/main.php

 'authClientCollection' => [
        'class' => \yii\authclient\Collection::class,
        'clients' => [
            'google' => [
                'class' => \yii\authclient\clients\Google::class,
                'clientId' => *********, //changed for issue purpose
                'clientSecret' => *********, //changed for issue purpose
                'returnUrl' => 'http://localhost/site/landing',
            ],
        ],
    ]

controllers/SiteController

    public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['logout', 'signup', 'auth'],
            'rules' => [
                [
                    'actions' => ['signup', 'auth'],
                    'allow' => true,
                    'roles' => ['?'],
                ],
                [
                    'actions' => ['logout'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'logout' => ['post'],
                'create-storyboard' => ['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, 'onAuthSuccess'],
        ],
    ];
}

public function onAuthSuccess($client)
{
    (new AuthHandler($client))->handle();
}

Upvotes: 0

Views: 746

Answers (2)

Gvep
Gvep

Reputation: 1206

I had solved the problem with the help from @Michal Hynčica. The problem was in my returnUrl which means with authentication url it must follow to authenticate rather the redirecting after authentication. So all I need to do was changing it to as below.

    'returnUrl' => 'http://localhost/site/auth?authclient=google'

Also don't forget to add same returnUrl to your google console's redirect url.

Upvotes: 0

Michal Hynčica
Michal Hynčica

Reputation: 6144

If you set returnUrl the user is from auth provider redirected directly to the url you've set in that property.

In your case the returnUrl says google, that it should redirect user to http://localhost/site/landing. But there is nothing in your site/landing action that would call the onAuthSuccess.

You need to let user come back to site/auth and redirect them after processing response from OAuth provider. To do that remove the returnUrl from config. That will make the authclient to use default return url which is the action that started the auth process.

Then modify your onAuthSuccess to redirect users to site/landing like this:

public function onAuthSuccess($client)
{
    (new AuthHandler($client))->handle();
    $this->redirect(['site/landing']);
}

Upvotes: 1

Related Questions