2amigos / yii2-usuario

Highly customizable and extensible user management, authentication, and authorization Yii2 extension
https://github.com/2amigos/yii2-usuario
Other
294 stars 141 forks source link

Argument 1 passed to Da\User\Event\SocialNetworkAuthEvent::__construct() must be an instance of Da\User\Model\SocialNetworkAccount, bool given #397

Closed dantart closed 3 weeks ago

dantart commented 3 years ago

When I use for FIRST time it works perfectly, and save record in the database.

But when I try to use it as second time (with an already registered user), I always have this error when coming back from the social network:

Argument 1 passed to Da\User\Event\SocialNetworkAuthEvent::__construct() must be an instance of Da\User\Model\SocialNetworkAccount, bool given

The URL at top is like this (in case of Google, but it happens with all social networks) https://mydomain.com/user/security/authclient/google?state=XXXXXXXXXXXXXXXXXXXXXXXX&code=XXXXXXXXXXXXXXXXXXXXXXXX&scope=email+profile+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&authuser=7&prompt=none#

Why is this?

It starts with the "SocialNetworkAuthEvent" object, but in some point , it's "lost" with a "false":

(this screenshot is with LinkedIn ... as I said, it happens with all social networks)

Captura de pantalla 2021-01-21 a las 16 22 12
dantart commented 3 years ago

Guys, this is insane ... after debugging a lot I fixed it with this:

adding return $account;

in line 95 of https://github.com/2amigos/yii2-usuario/blob/master/src/User/Service/SocialNetworkAccountConnectService.php

if not, that function returns "false" (when the account already exists in the social_account table), and that "false" is tried to be injected in the Da\User\Event\SocialNetworkAuthEvent class, therefore, the above error appears.

Can't believe this is happen just to me.

maxxer commented 3 years ago

I never used social auth, but indeed your fix looks correct...

Jonyx4 commented 3 years ago

Same happened here, worked with this fix in PHP 7.4.12.

Guys, this is insane ... after debugging a lot I fixed it with this:

adding return $account;

in line 95 of https://github.com/2amigos/yii2-usuario/blob/master/src/User/Service/SocialNetworkAccountConnectService.php

if not, that function returns "false" (when the account already exists in the social_account table), and that "false" is tried to be injected in the Da\User\Event\SocialNetworkAuthEvent class, therefore, the above error appears.

Can't believe this is happen just to me.

edegaudenzi commented 4 weeks ago

This is old but I've just stumbled on it today.

Happened when trying to connect to an already taken Social Network Account from a random logged User.

Solution is precisely what reported above in this thread. Here I'm just confirming:

replace return false with return $account in Da\User\Service\SocialNetworkAccountConnectService::getSocialNetworkAccount()

with this fix, you'll see this: image instead of image

Because Da\User\Service\SocialNetworkAccountConnectService::run() can move forward with the validation.

edegaudenzi commented 4 weeks ago

I would also add a note, even if maybe this is worth the creation of an additional issue.

When creating the new social record, only the client_id, data and provider fields get populated.

As an enhancement: Given we do have a Contract (aka Interface) called AuthClientInterface and that particular Interface imposes to add in the extra methods getEmail() and getUserName(), it would be ideal to use them in Da\User\Service\SocialNetworkAccountConnectService::getSocialNetworkAccount() to populate the email and username fields of the new social record.

This already happens for Da\User\Service\SocialNetworkAuthenticateService

maxxer commented 3 weeks ago

with this fix, you'll see this: image

This error is thrown when attempting a second registration? If so, make a PR and I'll merge