CakePHP + Facebook

Har*_*M V 1 facebook cakephp cakephp-1.3

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

我想用这种方式实现它

  1. 当用户访问网站时,他应该可以通过网站上的注册或Facebook Connect登录
  2. 现有用户应该能够将其帐户连接到他们的FB帐户
  3. 首次使用FB Connect登录该站点并且该站点上没有帐户的人员.应重定向到他们必须输入详细信息以完成配置文件的页面.

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

dec*_*eze 5

显然我在你之前做了同样的 ...... ;-)

这是我正在使用的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);
}
Run Code Online (Sandbox Code Playgroud)

这给了我一个数组,其中包含我可以从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'));
}
Run Code Online (Sandbox Code Playgroud)