Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Facebook Bundle Laravel Package

friendsofsymfony/facebook-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require friendsofsymfony/facebook-bundle
    

    (Note: Despite being deprecated, this bundle remains functional for legacy projects.)

  2. Configuration Add to config/packages/fos_facebook.yaml (Symfony 4+):

    fos_facebook:
        app_id: '%env(FACEBOOK_APP_ID)%'
        secret: '%env(FACEBOOK_APP_SECRET)%'
        scope: ['email', 'public_profile']
        graph_api_version: 'v12.0'
    
  3. First Use Case: Login Button Add a route to config/routes.yaml:

    fos_facebook_connect_connect: ~
    fos_facebook_connect_check: ~
    

    Render the login button in a Twig template:

    {{ render(controller('FOSFacebookBundle:Connect:connect', {'service_id': 'facebook'})) }}
    

Implementation Patterns

Authentication Workflow

  1. Service Configuration Extend fos_facebook.yaml to define custom user providers:

    fos_facebook:
        service:
            user_provider_id: 'app.user_provider.facebook'
    
  2. Custom User Provider Implement UserProviderInterface to map Facebook data to your user model:

    // src/User/FacebookUserProvider.php
    class FacebookUserProvider implements UserProviderInterface
    {
        public function loadUserByFacebookId($facebookId)
        {
            return User::where('facebook_id', $facebookId)->first();
        }
    }
    
  3. Post-Login Logic Use Symfony’s AuthenticationSuccessHandler to redirect or fetch additional data:

    // src/EventListener/FacebookLoginListener.php
    class FacebookLoginListener
    {
        public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
        {
            if ($providerKey === 'facebook') {
                $user = $token->getUser();
                $facebookData = $this->facebookManager->getUserData();
                // Merge or update user data
            }
        }
    }
    

Data Fetching Patterns

  • Graph API Calls Use the built-in FacebookManager to fetch user data:

    $userData = $this->facebookManager->getUserData();
    // Access fields like $userData['email'], $userData['name']
    
  • Permissions & Scopes Extend scopes dynamically in runtime:

    fos_facebook:
        scope: ['email', 'public_profile', '%env(FACEBOOK_EXTRA_SCOPES)%']
    

Gotchas and Tips

Common Pitfalls

  1. Deprecation Warnings

    • The bundle is deprecated; prefer hwi/oauth-bundle for new projects.
    • Legacy projects may still use this for simplicity, but expect no updates.
  2. Graph API Versioning

    • Always specify graph_api_version (e.g., v12.0). Facebook breaks backward compatibility frequently.
    • Monitor Facebook’s API changelog for deprecated fields.
  3. Token Expiry & Refresh

    • Short-lived tokens (1 hour) expire quickly. Use the offline_access scope (if available) for long-lived tokens.
    • Implement a TokenRefreshListener to handle token expiry gracefully.
  4. CSRF & Security

    • Ensure secret in fos_facebook.yaml matches your Facebook App settings.
    • Validate all Facebook responses server-side; never trust client-side data.

Debugging Tips

  • Enable Debug Mode Set debug: true in fos_facebook.yaml to log API errors:

    fos_facebook:
        debug: '%kernel.debug%'
    
  • Facebook SDK Logs Check Symfony’s var/log/dev.log for Facebook SDK errors (e.g., invalid app ID/secret).

  • Common Errors & Fixes

    Error Solution
    Invalid OAuth access token Regenerate the Facebook App token in App Dashboard.
    Missing required scope Add the missing scope to fos_facebook.scope.
    User not found Implement a fallback user creation logic in your UserProvider.

Extension Points

  1. Custom Fields Extend the FacebookManager to fetch non-standard fields:

    $customData = $this->facebookManager->get('/me?fields=custom_field', ['access_token' => $token]);
    
  2. Webhook Integration Use Facebook’s Graph API Webhooks to handle real-time events (e.g., likes, comments) via a Symfony controller:

    // src/Controller/FacebookWebhookController.php
    class FacebookWebhookController extends AbstractController
    {
        public function verify(Request $request)
        {
            $hubVerifyToken = $request->query->get('hub_verify_token');
            // Verify with Facebook and return challenge
        }
    
        public function handle(Request $request)
        {
            $data = json_decode($request->getContent(), true);
            // Process webhook payload
        }
    }
    
  3. Multi-Provider Setup Combine with other OAuth providers (e.g., Google) by configuring multiple services in fos_facebook.yaml:

    fos_facebook:
        service:
            facebook:
                app_id: '%env(FACEBOOK_APP_ID)%'
                secret: '%env(FACEBOOK_APP_SECRET)%'
            google:
                app_id: '%env(GOOGLE_CLIENT_ID)%'
                secret: '%env(GOOGLE_CLIENT_SECRET)%'
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views