I have a strange situation and I don't know how to debug it. One client requested to make a facebook login for a website that is on CakePHP version 1.2.12. So i used Facebook PHP SDK and followed standard procedure. If the user is not registered then register with the data from Facebook generate a password and so on. Then if the user is registered and he gets connected with his facebook account I want to use the CakePHP Auth Component to log him in. Problem is that it kicks me out as cannot authenticate. Here's what I did:
public function facebook_login_register() {
$fbUser = $this->Facebook->getUser();
if ($fbUser) {
//gets FB details about my profile
$fbUser = $this->Facebook->api('/me');
$registeredUser = $this->UserProfile->find('first', array('conditions' => array('UserProfile.email' => $fbUser['email']),
'fields' => array('UserProfile.email')));
if (!$registeredUser) {
... registration ...
}
else{
$usernameDisplay = $this->User->find('first', array('conditions' => array('User.username' => $fbUser['username']),
'fields' => array('User.username_display')));
}
$fb['username'] = $fbUser['username'];
$fb['password'] = $usernameDisplay['User']['username_display'];
$this->Auth->fields = array(
'username' => 'username',
'password' => 'username_display_encoded' //the encoded display_name (encoded with $this->Auth->password() or Security::hash(...). The result is correct when testing it for hashing)
);
if ($this->Auth->login($fb)) {
echo 'ok';
}
else{
echo 'not ok';
}
.... }
So when i am doing this it just kicks me out on the else branch.
Thanks in advance for your help! Cheers.
I think your login is failing because you have an incorrect field mapped to password - the username_display_encoded field doesn't even exist in the array $fb
which you're passing for the login. The array you intend to pass into $this->Auth->login()
needs to match the users model's fields for username and password, since it's going to check what you've passed against the database.
$user = $this->User->find('first', array('conditions' => array('User.username' => $fbUser['username'])));
$this->Auth->fields = array(
'username' => 'username',
'password' => 'username_display'
);
if ($this->Auth->login($user['User'])) {
echo 'ok';
}
else{
echo 'not ok';
}
Also, take a look at How do I integrate Facebook SDK login with cakephp 2.x? which seems like it might be useful, though it's for a newer version of Cake.