CakePHP + Facebook

发布于 2024-10-20 12:51:29 字数 372 浏览 3 评论 0原文

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

我想以这种方式实现它

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

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

I am trying to implement facebook Connect to my cakephp Application. i am using Nick's Facebook Plugin.

I wanna implement it this way

  1. When a user Visits the Site he should be able to login via Registration on the site or Facebook Connect
  2. Existing users should be able to connect their account to their FB account
  3. People who first time login to the site using FB Connect and dont have an account on the site. should be redirected to a page where they have to enter details to complete the profile.

What i have done -
I have followed the instruction of Nick to implement it and when i click Login - it connects to my app. but i dont understand how to create a username and password associated with the Fb Connect Id. and user it against the FB token.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2024-10-27 12:51:29

显然我正在做相同 事情在你之前......;-)

这是我正在使用的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'));
}

Apparently I'm doing the same thing a little before you... ;-)

Here's a method for Facebook login I'm using (slightly redacted and annotated):

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

This gives me an array with all the user details I could grab from Facebook and invokes ::oauthLogin, which either logs the user in with the given information or asks the user to fill in missing details and/or creates a new user record in the database. The most important part you get from the Facebook API is the $userInfo['id'] and/or email address, either of which you can use to identify the user in your database. If you're using the AuthComponent, you can "manually" log in the user using $this->Auth->login($user_id), where $user_id is the id of the user in your own database.


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'));
}
幸福还没到 2024-10-27 12:51:29

别再看了。这是一篇出色的文章,它将全程指导您(不包括任何现成的插件):
将 Facebook Connect 与 CakePHP 的身份验证组件集成

只需遵循其中描述的方法即可。

干杯,
米^e

Look no further. Here is an excellent article that'll guide you all the way through (minus any readymade plugins):
Integrating Facebook Connect with CakePHP's Auth component

Simply follow the approach described in there.

Cheers,
m^e

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文