CakePHP-Facebook-Plugin icon indicating copy to clipboard operation
CakePHP-Facebook-Plugin copied to clipboard

__syncFacebookUser does not account for an existing "site" user

Open starchildno1 opened this issue 13 years ago • 3 comments

//attempt to find user by facebook_id- has no commonality with the user table as a whole. So if an already registered user has the same email as a facebook account, then a new account will be created and not an update of the existing account with the facebook_id.

facebook_id is only used to auto login a site user who is already logged into facebook. Therefore, finding a user by email will associate the user table with the facebook account. This goes in the else condition after // check if the user already has an account // User is logged in but doesn't have a facebook id condition

        // attempt to find the user by their email (not facebook id)
        $this->authUser = $this->User->findByEmail($this->me['email']);
        //if we have a user, set hasAccount
        if(!empty($this->authUser)){
            //but they don't have a facebook id then update facebook_id field
            if (empty($this->authUser['User']['facebook_id'])) {
                $this->User->id = $this->authUser['User']['id'];
                $this->User->saveField('facebook_id', $this->uid);
            }
            $this->hasAccount = true;
        }

$this->me has to be set in the initialize function to get the email.

$this->me = $this->FB->api('/me');

It will be reset in the user function by $this->me = $this->Controller->Session->read

Now if an existing user that signed up with your site account activation, decides to login with facebook to share, like and whats not. A totally new user won't be created in the user table and f things up.

starchildno1 avatar Jun 18 '12 02:06 starchildno1

I'd like this added, too.

aldeed avatar Dec 11 '12 00:12 aldeed

In case anyone else cares about this, you can match up new FB accounts with current user accounts in beforeFacebookSave() like so:

function beforeFacebookSave() {
        $emailAddress = $this->Connect->user('email');
        if (empty($emailAddress)) {
            $uid = $this->Connect->user('username');
            if (empty($uid)) {
                $uid = $this->Connect->user('id');
            }
            $emailAddress = "[email protected]";
        }

        $this->Connect->authUser['User']['username'] = strtolower($emailAddress);

        //match with existing user with this username (e-mail) if there is one
        //specifying the ID will cause this to update that user record instead of creating a new one
        $this->loadModel('User');
        $existingUser = $this->User->findByUsername($this->Connect->authUser['User']['username']);
        if ($existingUser) {
            $this->Connect->authUser['User']['id'] = $existingUser['User']['id'];
            unset($this->Connect->authUser['User']['password']); //clear the password so that the user's current password isn't overwritten
        }
        return true;
}

Note that I save the e-mail as the username in this example, but you could swap all the "username"s with "email" if you have a separate email database column.

aldeed avatar Jan 31 '13 20:01 aldeed

Very good aldeed.

This is a really needed and basic feature that should have already come inside the plugin. I was implementing it, when i saw that you already did it.

vsouto avatar Feb 04 '13 11:02 vsouto