amnah / yii2-user

Yii2 user authentication module
MIT License
253 stars 104 forks source link

Set url_callback in yii2-user with facebook #151

Closed famigone closed 8 years ago

famigone commented 8 years ago

There is any way to set a url_callback in a Facebook aOuth login? I need to redirect the user to a specific url after login.

amnah commented 8 years ago

I think you need to set the returnUrl. Try this:

'facebook' => [
                'class' => 'yii\authclient\clients\Facebook',
                'clientId' => 'xxxxxxxxxx',
                'clientSecret' => 'yyyyyyyyyy',
                'scope' => 'email',
                'returnUrl' => '/some/page',
            ],
famigone commented 8 years ago

Amnah, i was looking into AuthController's action in order to solve this issues... i have added a new action to controller with a diferent succesUrl parameter and set it from the the new login view, but it doesn't work.

This is the action section of my AuthController

class AuthController extends Controller
{
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'connect' => [
                'class' => 'yii\authclient\AuthAction',
                'successCallback' => [$this, 'connectCallback'],
                'successUrl' => Yii::$app->homeUrl . 'user/account',
            ],
            'login' => [
                'class' => 'yii\authclient\AuthAction',
                'successCallback' => [$this, 'loginRegisterCallback'],
                'successUrl' => $this->getUrl(),
            ],
            'loginadm' => [
                'class' => 'yii\authclient\AuthAction',
                'successCallback' => [$this, 'loginRegisterCallback'],
                'successUrl' => Yii::$app->homeUrl,
            ],

        ];
    }

And this is my social button in a view login:

 <?php if (Yii::$app->get("authClientCollection", false)): ?>
        <div class="col-lg-offset-2">
            <?= yii\authclient\widgets\AuthChoice::widget([
                'baseAuthUrl' => ['/user/auth/loginadm']
            ]) ?>
        </div>
    <?php endif; ?>

In all cases, the action called is login, but never loginadm, like i wish.

amnah commented 8 years ago

Hmmm that looks correct. Are you sure it's using the correct view file?

What's the url that you get from facebook?

For example if you go to my demo and click the facebook login button, it will send you to this url:

https://www.facebook.com/login.php?skip_api_login=1&api_key=311131662375942&signed_next=1&next=https%3A%2F%2Fwww.facebook.com%2Fv2.0%2Fdialog%2Foauth%3F redirect_uri%3Dhttp%253A%252F%252Fyii2.amnahdev.com%252Fuser%252Fauth%252Flogin %253Fauthclient%253Dfacebook%26display%3Dpopup%26scope%3Demail%26response_type%3Dcode%26client_id%3D311131662375942%26ret%3Dlogin%26logger_id%3D06f8857a-7556-46c4-b5f4-6e060b1beba1&cancel_url=http%3A%2F%2Fyii2.amnahdev.com%2Fuser%2Fauth%2Flogin%3Fauthclient%3Dfacebook%26error%3Daccess_denied%26error_code%3D200%26error_description%3DPermissions%2Berror%26error_reason%3Duserdenied%23%3D_&display=popup&locale=en_US&logger_id=06f8857a-7556-46c4-b5f4-6e060b1beba1

Note the redirect_uri which correlates to baseAuthUrl

famigone commented 8 years ago

Yes, i finally find out the problem, you are right. This is my conclusion about (I would appreciate if you confirm): All the standalone actions are performed regardless of whether they are invoked or not, unlike the inline actions. So... i had standalone actions performed without having had invoked. In particular, in all actions, the method $this->getUrl() was performed (which made me think he was running an unwanted action), becouse i had the line 'successUrl' => $this->getUrl(), in one action. Thanks for your support man.

amnah commented 8 years ago

Hmmm yes that does look a bit weird. What exactly is in your $this->getUrl() function? If it's calling another action that seems wrong ...

famigone commented 8 years ago

It just read a environment variable. This variable will not be available (created) for all actios, just for one.

public function getUrl() { if (empty(Yii::$app->session->get('EVENTONOADMIN'))) $ret = ''; else $ret = Evento::findOne(Yii::$app->session->get('EVENTONOADMIN'))->nombrecorto; return $ret; }

But, for example, if getUrl() do this: $error= 4/0;

It always is invoqued, and always fails. Even if i don't call that actions. I think the standalone actios works diferent from inline actions.