Open starchildno1 opened 12 years ago
I'd like this added, too.
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 = "$uid@facebook.com";
}
$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.
//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
$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.