FriendsOfSymfony / FOSFacebookBundle

NOT MAINTAINED - see https://github.com/hwi/HWIOAuthBundle
322 stars 140 forks source link

Bug in FacebookProvider (trying to register user already existing with same email) #298

Closed topwebstudio closed 10 years ago

topwebstudio commented 10 years ago

Hi folks. I think that the bundle has small bug in

FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider

in createAuthenticatedToken method where

$user = $this->userProvider->loadUserByUsername($uid);

I think this is insufficient because in real life you have users registering through other providers (such as FOSUserBundle) directly. If such user tries to log in through Facebook (but already has an account registered with other provider) an error will occur:

" Integrity constraint violation: 1062 Duplicate entry 'email@email.com' for key 'UNIQ_.......'"

I think that there should also be a check in createAuthenticatedToken if user with the same email address is not already registered and update Facebook stuff only (FACEBOOK_ROLE, facebookId) without trying to create the user again to avoid the duplicate key error.

diegoholiveira commented 10 years ago

I have the same scenario here, and there's nothing wrong with the bundle. What do you need to do is write your own provider to treat if the user exists or not in your database.

topwebstudio commented 10 years ago

To fix this I've added in the FacebookProvider (taken from the docs):

        try {
            $fbdata = $this->facebook->api('/me');
        } catch (FacebookApiException $e) {
            $fbdata = null;
        }

And after those lines the needed fix:

        if (empty($user) and !empty($fbdata)) {
            $user = $this->userManager->findUserBy(array('email' => $fbdata['email']));
        }