Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ahmed Alaa's avatar

Integrate Facebook with Laravel11-Vue3 App

Hello All I'm creating a social media planner using Vue3 and Laravel11 When I used the Facebook SDK for PHP, I got CORS error when I was trying to redirect the user to the Facebook auth page. Here's my code: 1- FacebookServiceProvider

<?php

namespace App\Providers;

use Facebook\Facebook;
use Illuminate\Support\ServiceProvider;

class FacebookServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        $this->app->singleton(Facebook::class, function () {
            return new Facebook([
                'app_id' => config('services.facebook.app_id'),
                'app_secret' => config('services.facebook.app_secret'),
                'default_graph_version' => config('services.facebook.default_graph_version'),
            ]);
        });
    }
}

2- FacebookController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\FacebookService;
use Facebook\Facebook;

class FacebookController extends Controller
{

    public function __construct(private Facebook $fb)
    {
    }

    /**
     * Connect a user with Facebook.
     */
    public function connect(Request $request)
    {
        $loginUrl = FacebookService::getFacebookLoginUrl($this->fb);

        return redirect()->away($loginUrl);
    }
}

3- FacebookService:

<?php

namespace App\Services;

use Facebook\Facebook;

class FacebookService
{
    /**
     * Create a new class instance.
     */
    public function __construct()
    {
    }

    /**
     * Get Facebook login URL 
     * @param Facebook $fb
     * @return string
     */
    public static function getFacebookLoginUrl(Facebook $fb): string
    {
        $helper = $fb->getRedirectLoginHelper();

        $permissions = ['email'];

        $loginUrl = $helper->getLoginUrl(config('services.facebook.redirect_url'), $permissions);

        return $loginUrl ?? '';
    }
}
  • Here's the error
Access to XMLHttpRequest at 'https://www.facebook.com/v19.0/dialog/oauth?client_id=*******&state=dc116e489af76f67ec24433bea3d81e8&response_type=code&sdk=php-sdk-5.1.4&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fapi%2Fv1%2Ffacebook%2Fcallback&scope=email' (redirected from 'http://localhost:8000/api/v1/facebook/connect') from origin 'http://localhost:8000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Thanks in advance

0 likes
2 replies
Ahmed Alaa's avatar

@gych Yes, Currently I have these configurations


    'paths' => ['api/*', 'sanctum/csrf-cookie'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => false,

Please or to participate in this conversation.