0

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();
}
Gvep
  • 1,196
  • 2
  • 9
  • 18

2 Answers2

1

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']);
}
Michal Hynčica
  • 5,038
  • 1
  • 12
  • 24
  • Hello Michael, thanks for the response but removing returnUrl give redirect_uri_mismatch error. It should be available in main config. Any other idea ? – Gvep Sep 17 '19 at 14:11
  • That's probably because the authorized redirect uri set in google's api console doesn't match the one that auth action uses. You've probably set it to `http://localhost/site/landing` but you need to add the url that auth action uses. It's probably `http://localhost/site/auth?authclient=google` – Michal Hynčica Sep 17 '19 at 14:56
  • I just solved the problem, thank you Michal, your help is appreciated. – Gvep Sep 17 '19 at 14:59
0

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.

Gvep
  • 1,196
  • 2
  • 9
  • 18