FOSFacebookBundle icon indicating copy to clipboard operation
FOSFacebookBundle copied to clipboard

The user and email exists in the bd

Open Chrysweel opened this issue 13 years ago • 4 comments

What happens when is the user already registered in the database?

I did that this bundle works. But now I have a problem. An user which exist in my web, when he log with facebook, he get an error: this email xxx already exists in the bd

How I can solve this problem?? And associate the users with its count of facebook...

Thanks! ; )

Chrysweel avatar Oct 11 '12 14:10 Chrysweel

It depends of what you are doing in your own code. FOSFacebookBundle itself does not use any database.

stof avatar Oct 11 '12 17:10 stof

thank @stof for reply!

I have FOSUserBundle and FosFacebookBundle.

And when I log with facebook, I get the error: This email exist.

How can I associate an user with facebook ID ? Any idea? Thanks!

Chrysweel avatar Oct 11 '12 17:10 Chrysweel

Anyone ? pleeease any idea ?

Chrysweel avatar Oct 15 '12 08:10 Chrysweel

In your Facebook provider, in the loadUserByUsername function:

   public function loadUserByUsername($username)
   {
    $user = $this->findUserByFbId($username);

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

    if (!empty($fbdata)) {

        $userByEmail = $this->userManager->findUserBy(array('email' => $fbdata['email']));

        if (!empty($userByEmail)) {
            $user = $userByEmail;
        }
        elseif (empty($user)) {
            $user = $this->userManager->createUser();
            $user->setEnabled(true);
            $user->setPassword('');
        }

        // TODO use http://developers.facebook.com/docs/api/realtime
        $user->setFBData($fbdata);

        if (count($this->validator->validate($user, 'Facebook'))) {
            // TODO: the user was found obviously, but doesnt match our expectations, do something smart
            throw new UsernameNotFoundException('The facebook user could not be stored');
        }
        $this->userManager->updateUser($user);
    }

    if (empty($user)) {
        throw new UsernameNotFoundException('The user is not authenticated on facebook');
    }

    return $user;
}

And you can after change your setFbData in your User class if you don't want the username and salt to be updated.

Toomaie avatar Oct 25 '12 10:10 Toomaie