【问题标题】:CakePHP + FacebookCakePHP + Facebook
【发布时间】:2011-03-04 06:25:52
【问题描述】:

我正在尝试实现 facebook Connect 到我的 cakephp 应用程序。我正在使用 Nick 的 Facebook 插件。

我想这样实现

  1. 当用户访问网站时,他应该能够通过在网站上注册或 Facebook Connect 登录
  2. 现有用户应该能够将他们的帐户连接到他们的 FB 帐户
  3. 首次使用 FB Connect 登录网站但没有网站帐户的人。应该被重定向到他们必须输入详细信息才能完成个人资料的页面。

我做了什么—— 我按照尼克的指示实施它,当我点击登录时 - 它连接到我的应用程序。但我不明白如何创建与 Fb Connect Id 关联的用户名和密码。并将其用于 FB 令牌。

【问题讨论】:

    标签: cakephp facebook cakephp-1.3


    【解决方案1】:

    显然我在你之前做samething... ;-)

    这是我正在使用的 Facebook 登录方法(略有编辑和注释):

    public function facebook($authorize = null) {
        App::import('Lib', 'Facebook.FB');
        $Fb = new FB();
    
        $session = $Fb->getSession();
    
        // not logged into Facebook and not a callback either,
        // sending user over to Facebook to log in
        if (!$session && !$authorize) {
            $params = array(
                'req_perms'  => /* the permissions you require */,
                'next'       => Router::url(array('action' => 'facebook', 'authorize'), true),
                'cancel_url' => Router::url(array('action' => 'login'), true)
            );
            $this->redirect($Fb->getLoginUrl($params));
        }
    
        // user is coming back from Facebook login,
        // assume we have a valid Facebook session
        $userInfo = $Fb->api('/me');
    
        if (!$userInfo) {
            // nope, login failed or something went wrong, aborting
            $this->Session->setFlash('Facebook login failed');
            $this->redirect(array('action' => 'login'));
        }
    
        $user = array(
            'User' => array(
                'firstname'       => $userInfo['first_name'],
                'lastname'        => $userInfo['last_name'],
                'username'        => trim(parse_url($userInfo['link'], PHP_URL_PATH), '/'),
                'email'           => $userInfo['email'],
                'email_validated' => $userInfo['verified']
            ),
            'Oauth' => array(
                'provider'        => 'facebook',
                'provider_uid'    => $userInfo['id']
            )
        );
    
        $this->oauthLogin($user);
    }
    

    这为我提供了一个数组,其中包含我可以从 Facebook 获取的所有用户详细信息并调用 ::oauthLogin,它可以使用给定信息登录用户或要求用户填写缺失的详细信息和/或创建一个新用户记录在数据库中。您从 Facebook API 获得的最重要部分是 $userInfo['id'] 和/或电子邮件地址,您可以使用其中任何一个来识别数据库中的用户。如果您使用的是 AuthComponent,您可以使用 $this->Auth->login($user_id)“手动”登录用户,其中 $user_id 是您自己数据库中的用户 ID。


    private function oauthLogin($data) {
        $this->User->create();
    
        // do we already know about these credentials?
        $oauth = $this->User->Oauth->find('first', array('conditions' => $data['Oauth']));
    
        if ($oauth) {
            // yes we do, let's try to log this user in
            if (empty($oauth['User']['id']) || !$this->Auth->login($oauth['User']['id'])) {
                $this->Session->setFlash('Login failed');
            }
            $this->redirect('/');
        }
    
        // no we don't, let's see if we know this email address already
        if (!empty($data['User']['email'])) {
            $user = $this->User->find('first', array('conditions' => array('email' => $data['User']['email'])));
            if ($user) {
                // yes we do! let's store all data in the session
                // and ask the user to associate his accounts
    
                $data['User'] = array_merge($data['User'], $user['User']);
                $data['Oauth']['user_id'] = $user['User']['id'];
    
                $this->Session->write('Oauth.associate_accounts', $data);
                $this->redirect(array('action' => 'oauth_associate_accounts'));
            }
        }
    
        // no, this is a new user, let's ask him to register        
        $this->Session->write('Oauth.register', $data);
        $this->redirect(array('action' => 'oauth_register'));
    }
    

    【讨论】:

    • 你在使用 Nick 的插件吗?还是你导入 fb 库并自己做?
    • @Harsha 是的,这是我昨天推荐给你的 Nick 的插件。在最顶层,我是App::importing FB 课程,这就是所有必要的。 (我还在这个控制器中包含了'Facebook.Connect' 组件。)
    • 可以通过 IM 与您联系吗?我对此有点迷茫和困惑。你能给我发一封电子邮件吗harshamv@varialbe3.com
    • @Harsha 基本同上。只是不要在最后使用检索到的信息登录,您只需将令牌保存在当前用户的数据库中。
    • @Harsha 查看更新。我会让你弄清楚oauth_registeroauth_associate_accounts 操作。作为背景信息,每个User hasMany 关联Oauth 记录。 Oauth 记录只存储提供者 ID(例如“Facebook”)和提供者提供的唯一 ID。
    【解决方案2】:

    别再看了。这是一篇出色的文章,将指导您一路走来(减去任何现成的插件):
    Integrating Facebook Connect with CakePHP's Auth component

    只需按照其中描述的方法即可。

    干杯, m^e

    【讨论】:

    • 谢谢老兄。会看看它:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多