2017 © Pedro Peláez
 

library facebook-service-provider

Facebook API and connect integration into your Silex applications

image

grom/facebook-service-provider

Facebook API and connect integration into your Silex applications

  • Saturday, March 8, 2014
  • by GromNaN
  • Repository
  • 1 Watchers
  • 26 Stars
  • 1,393 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 1 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

FacebookServiceProvider

The FacebookServiceProvider adds Facebook Connect and API to your applications., (*1)

It integrates FOSFacebookBundle into Silex., (*2)

Parameters

  • facebook.config: Configuration of your Facebook application. (appId, secret, fileUpload, )
  • facebook.permissions: List of permissions required to connect.
  • facebook.session_prefix: Prefix for Facebook data.

Services

Installation

Using composer :, (*3)

composer require grom/facebook-service-provider

Registering

First, you must register the SecurityServiceProvider., (*4)

use Silex\Provider\FacebookServiceProvider;

$app->register(new FacebookServiceProvider(), array(
    'facebook.config' => array(
        'appId'      => 'YOUR_APP_ID',
        'secret'     => 'YOUR_APP_SECRET',
        'fileUpload' => false, // optional
    ),
    'facebook.permissions' => array('email'),
));

Authentication

To authenticate users with Facebook Connect, first you need to register the SecurityServiceProvider., (*5)

To enable Facebook authentication, just add a "facebook" option to your firewall configuration., (*6)

$app['security.firewalls'] = array(
    'private' => array(
        'pattern' => '^/',
        'facebook' => array(
            'check_path' => '/login_check',
            'login_path' => '/login',
        ),
        // Users are identified by their Facebook UID
        'users' => array(
            // This is Mark Zuckerberg
            '4' => array('ROLE_USER', null),
        ),
    ),
);

If you don't set a login_path, the user is redirected to Facebook., (*7)

Developers of embedded Facebook Applications may define app_url, server_url and display options., (*8)

Defining a custom User Provider and automatic user creation

The UserProvider used to find Facebook user is similar to the username/password UserProvider. The differences are that users are identified by their Facebook UID instead of their username., (*9)

If the Facebook UID is not found in your database, the user provider can create the user automatically. You simply have to implements the method FOS\FacebookBundle\Security\User\UserProviderInterface::createUserFromUid, (*10)

use FOS\FacebookBundle\Security\User\UserManagerInterface as FacebookUserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Doctrine\DBAL\Connection;

class FacebookUserProvider implements FacebookUserProviderInterface
{
    private $conn;

    public function __construct(Connection $conn)
    {
        $this->conn = $conn;
    }

    public function loadUserByUsername($uid)
    {
        $stmt = $this->conn->executeQuery('SELECT * FROM users WHERE username = ?', array($uid));

        if (!$user = $stmt->fetch()) {
            throw new UsernameNotFoundException(sprintf('Facebook UID "%s" does not exist.', $uid));
        }

        return new User($user['username'], null, explode(',', $user['roles']), true, true, true, true);
    }

    public function refreshUser(UserInterface $user)
    {
        if (!$user instanceof User) {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
        }

        return $this->loadUserByUsername($user->getUsername());
    }

    public function createUserFromUid($uid)
    {
        $this->conn->insert('users', array(
            'username' => $uid,
            'roles'    => 'ROLE_USER',
        ));

        return $this->loadUserByUsername($uid);
    }

    public function supportsClass($class)
    {
        return $class === 'Symfony\Component\Security\Core\User\User';
    }
}

Now use your custom user provider., (*11)

$app['security.firewalls'] = array(
    'default' => array(
        'facebook' => array(
        ),
        'users' => $app->share(function () use ($app) {
            return new FacebookUserProvider($app['db']);
        }),
    ),
);

Facebook Graph API

Once a user is authenticated with Facebook, you can make Facebook Graph API requests., (*12)

$app->get('/', function () use ($app) {
    $user = $app['facebook']->api('/me');

    return 'Welcome ' . $user['name'];
});

Look at the Facebook Graph Explorer., (*13)

The Versions

08/03 2014

dev-master

9999999-dev

Facebook API and connect integration into your Silex applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jérôme Tamarelle

08/03 2014

v1.1.0

1.1.0.0

Facebook API and connect integration into your Silex applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jérôme Tamarelle

02/03 2013

v1.0.0

1.0.0.0

Facebook API and connect integration into your Silex applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jérôme Tamarelle

02/03 2013

v1.0.0-RC1

1.0.0.0-RC1

Facebook API and connect integration into your Silex applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jérôme Tamarelle