lzakrzewski/facebook-authentication-bundle

这个组件可以帮助您将“使用Facebook登录”功能添加到您的应用中

1.0.5 2016-02-07 16:20 UTC

This package is not auto-updated.

Last update: 2024-09-20 18:48:03 UTC


README

Build Status Latest Stable Version Total Downloads

此组件为您的Symfony2应用提供Facebook身份验证,使用FOSUserBundle。目标:保持最小化,并使用Symfony2和FOSUserBundle中的现有组件。

功能

  • 启用应用中的Facebook登录功能
  • 将来自Facebook的数据创建的用户添加到您的应用

要求

    "require": {
        "php": ">=5.4",
        "friendsofsymfony/user-bundle": "~2.0@dev",
        "lzakrzewski/facebook-authentication-adapter": "~1.0"
    }

支持的Facebook API版本

  • v2.5

安装

步骤1:将FOSUserBundle集成到您的应用中

阅读master文档.

步骤2:使用Composer要求FacebookAuthenticationBundle

composer require lzakrzewski/facebook-authentication-bundle "~1.0"

步骤3:启用FacebookAuthenticationBundle

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new Lzakrzewski\FacebookAuthenticationBundle\LzakrzewskiFacebookAuthenticationBundle(),
        // ...
    );
}

步骤4:使用FacebookUser实现您的User类

<?php
// src/AppBundle/Entity/User.php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Lzakrzewski\FacebookAuthenticationBundle\Model\FacebookUser;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User extends BaseUser implements FacebookUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="bigint", nullable=true)
     */
    private $facebookId;

    /** {@inheritdoc} */
    public function getFacebookId()
    {
        return $this->facebookId;
    }

    /** {@inheritdoc} */
    public function setFacebookId($facebookId)
    {
        $this->facebookId = $facebookId;
    }
}

注意 存储FacebookId的字段应命名为 facebookId 或使用注释 FacebookIdFacebookId注释

步骤5:配置config.yml

最小配置
lzakrzewski_facebook_authentication:
    app_id: 1234
    app_secret: secret

参数:需要app_idsecret来获取访问令牌:访问令牌

完整配置示例
lzakrzewski_facebook_authentication:
    app_id: 1234
    app_secret: secret
    scope: ["public_profile", "email", "user_birthday"]
    fields: ["name", "email", "birthday"]

参数

  • scope 权限数组:使用Facebook登录的权限,
  • fields 默认情况下,当您进行查询时,节点或边缘中不是所有字段都会返回。您可以使用“fields”查询参数选择要返回的字段(或边缘)。 选择字段

注意

  • scope 应包含 public_profileemail
  • fields 应包含 nameemail

步骤6:配置routing.yml

facebook_login_path:
    pattern: /facebook/login

步骤7:在security.yml中启用facebook_listener

# app/config/security.yml
security:
    # ...
    
    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4

            logout:       true
            anonymous:    true
            # Enable facebook_listener  
            lzakrzewski_facebook: true
            
    # ...

步骤8:更新您的数据库模式

php app/console doctrine:schema:update --force

步骤9:设置您的Facebook应用

文档

现在当请求路由/facebook/login时,将执行代码交换过程 代码交换

更多文档