CakePHP Auth Facebook Connect无法正常工作(无插件)

时间:2014-01-09 15:51:15

标签: php facebook facebook-graph-api cakephp

我有一个奇怪的情况,我不知道如何调试它。 一位客户要求为CakePHP版本1.2.12上的网站登录facebook。 所以我使用Facebook PHP SDK并遵循标准程序。如果用户未注册,则使用Facebook注册数据生成密码等。 然后,如果用户已注册并且他与他的Facebook帐户连接,我想使用CakePHP Auth组件登录他。 问题是,它因为无法验证而踢我。 这是我做的:

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';
            }

.... }

因此,当我这样做时,它只是把我踢出了其他分支。

提前感谢您的帮助! 欢呼声。

1 个答案:

答案 0 :(得分:1)

我认为您的登录失败是因为您将错误的字段映射到密码 - username_display_encoded字段甚至不存在于您为登录传递的数组$fb中。您打算传递到$this->Auth->login()的数组需要匹配用户模型的用户名和密码字段,因为它将检查您对数据库传递的内容。

$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';
}

另外,看看How do I integrate Facebook SDK login with cakephp 2.x?似乎可能有用,虽然它适用于较新版本的Cake。

相关问题