gpt4 book ai didi

facebook-graph-api - 如何将 Facebook SDK 登录与 cakephp 2.x 集成?

转载 作者:行者123 更新时间:2023-12-04 02:39:51 24 4
gpt4 key购买 nike

关于将 Facebook 登录与在线 cakephp Auth 组件集成的最新资源似乎很少甚至没有。我找到了以下资源:

  • Bakery Article使用cakephp 1.3?和旧版本的 Facebook SDK
  • Cakephp Plugin by webtechnick似乎正在开发中

  • 除此之外,我没有找到确定的资源。我希望集成尽可能灵活(不使用魔术插件)。因此,经过大量研究,我终于得出了一个不错的解决方案,我今天在这里分享。请贡献,因为我对蛋糕很陌生。

    最佳答案

    Cakephp 2.x Auth 与 Facebook Auth 的集成以实现无缝用户身份验证

    首先,您应该阅读奇妙的 cakePHP Auth Component并关注 Simple Authentication and Authorization Application tutorial来自 cakephp book 2.x(假设您还学习了本系列的前两个教程。完成后,您应该已经成功构建了一个具有用户身份验证和授权的简单 cakePHP 应用程序。

    接下来你应该下载 facebook SDK并获得 App ID来自 Facebook 。

    首先,我们将 Facebook sdk 复制到 App/Vendors。然后我们将在 AppController beforeFilter 中导入并初始化它方法。

    //app/Controller/AppController.php

    public function beforeFilter() {
    App::import('Vendor', 'facebook-php-sdk-master/src/facebook');
    $this->Facebook = new Facebook(array(
    'appId' => 'App_ID_of_facebook',
    'secret' => 'App_Secret'

    ));

    $this->Auth->allow('index', 'view');
    }

    我们正在 AppController 中初始化 Facebook SDK,以便我们可以在整个应用程序中访问它。接下来我们将使用 SDK 生成 Facebook 登录 URL 并将其传递给 View 。我通常在 beforeRender 中执行此操作方法。

    注意:以上配置细节(appId & secret)最好保存在 App/Config/facebook.php 中。然后你应该使用 cake Configure .
    //app/Controller/AppController.php

    public function beforeRender() {
    $this->set('fb_login_url', $this->Facebook->getLoginUrl(array('redirect_uri' => Router::url(array('controller' => 'users', 'action' => 'login'), true))));
    $this->set('user', $this->Auth->user());
    }

    我们将更新我们的布局,以便我们可以为所有尚未登录的用户显示此 facebook 登录链接。请注意我们如何设置 redirect_uri到我们的应用程序用户/登录操作。这样一旦 facebook 对用户进行了身份验证,我们也可以使用 cake::Auth 登录他。这样做有很多好处, including the solution for this question .
    <!-- App/Views/Layouts/default.ctp just after <div id="content"> -->
    <?php
    if($user) echo 'Welcome ' . $user['username'];
    else {
    echo $this->Html->link('Facebook Login', $fb_login_url) . ' | ';
    echo $this->Html->link('Logout', array('controller' => 'user', 'action' => 'logout'));
    ?>

    当用户单击登录链接时,facebook SDK 将登录用户并将其重定向到我们的应用程序 Users/login。我们将更新此操作以处理此问题:
    // App/Controller/UsersController.php
    // Handles login attempts from both facebook SDK and local
    public function login()
    {
    // If it is a post request we can assume this is a local login request
    if ($this->request->isPost()){
    if ($this->Auth->login()){
    $this->redirect($this->Auth->redirectUrl());
    } else {
    $this->Session->setFlash(__('Invalid Username or password. Try again.'));
    }
    }

    // When facebook login is used, facebook always returns $_GET['code'].
    elseif($this->request->query('code')){

    // User login successful
    $fb_user = $this->Facebook->getUser(); # Returns facebook user_id
    if ($fb_user){
    $fb_user = $this->Facebook->api('/me'); # Returns user information

    // We will varify if a local user exists first
    $local_user = $this->User->find('first', array(
    'conditions' => array('username' => $fb_user['email'])
    ));

    // If exists, we will log them in
    if ($local_user){
    $this->Auth->login($local_user['User']); # Manual Login
    $this->redirect($this->Auth->redirectUrl());
    }

    // Otherwise we ll add a new user (Registration)
    else {
    $data['User'] = array(
    'username' => $fb_user['email'], # Normally Unique
    'password' => AuthComponent::password(uniqid(md5(mt_rand()))), # Set random password
    'role' => 'author'
    );

    // You should change this part to include data validation
    $this->User->save($data, array('validate' => false));

    // After registration we will redirect them back here so they will be logged in
    $this->redirect(Router::url('/users/login?code=true', true));
    }
    }

    else{
    // User login failed..
    }
    }
    }

    我们完成了!如您所见,大部分繁重的工作都是通过此操作完成的。您最好将上面的一些代码移到 UserModel 中。所以这里是正在发生的事情的摘要。

    首先,我们检查登录请求是否是从我们的应用程序@Users/login 的登录表单发送的。如果是,那么我们只需让用户登录。否则我们检查该用户是否存在于我们的数据库中,以及他是否登录或创建了一个新用户,然后让他登录。

    小心在这里验证用户,而不仅仅是他们的电子邮件,比如他们的 facebook_id。否则,用户可能会更改他们的 Facebook 电子邮件并劫持您应用程序的另一个用户。

    快乐编码!

    关于facebook-graph-api - 如何将 Facebook SDK 登录与 cakephp 2.x 集成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18037971/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com