douxunzui1519 2014-01-09 15:51
浏览 61
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • duandangqin0559 2014-01-09 21:30
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值