Search code examples
facebookfacebook-graph-apilaravellaravel-socialite

Laravel 5.1 - Facebook Authentication through Socialite


I am trying to implement Facebook Login to my project however I receive an error:

ClientException in Middleware.php line 69: Client error: 400

  1. in /Applications/MAMP/htdocs/laratest/vendor/guzzlehttp/guzzle/src/Middleware.php line 69

  2. at Middleware::GuzzleHttp{closure}(object(Response)) in Promise.php line 199

  3. at Promise::callHandler('1', object(Response), array(object(Promise), object(Closure), null)) in Promise.php line 152

  4. at Promise::GuzzleHttp\Promise{closure}() in TaskQueue.php line 60

  5. at TaskQueue->run() in CurlMultiHandler.php line 96


Steps I've gone through:

  • composer require laravel/socialite & composer update.

  • In my config>services.app,

'facebook' => [ 'client_id' => env('FB_CLIENT_ID'), 'client_secret' => env('FB_SECRET_ID'), 'redirect' => 'http://localhost.com:8888', ],

  • Added Laravel\Socialite\SocialiteServiceProvider::class, & 'Socialite' => Laravel\Socialite\Facades\Socialite::class, in config>app.php

  • Set up the routes successfully.

Route::get('auth/facebook', 'Auth\AuthController@redirectToProvider'); Route::get('auth/facebook', 'Auth\AuthController@handleProviderCallback');

  • Setup the link successfully in my blade file. Login with Facebook

  • In my Controller>AuthController.php, I added:

    use Laravel\Socialite\Facades\Socialite;
    
    ** Beside everything that AuthController has, inside the AuthController class, I added:**
    
      public function redirectToProvider()
       {
        return Socialite::driver('facebook')
             ->scopes(['scope1', 'scope2'])->redirect();
       }
    
    /**
     * Obtain the user information from Facebook.
     *
     * @return Response
     */
    
       public function handleProviderCallback()
        {
          $user = Socialite::driver('facebook')->user();
    
        // $user->token;
        }
    
  • Also users table:

       public function up()
        {
           Schema::create('users', function (Blueprint $table) {
              $table->increments('id');
              $table->string('username');
              $table->string('name');
              $table->string('email')->unique();
              $table->string('password', 60);
              $table->string('avatar');
              $table->string('provider');
              $table->string('provider_id');
              $table->rememberToken();
              $table->timestamps();
                });
        }
    

Edit: When I comment out $user = Socialite::driver('facebook')->user(); part, I get redirected to localhost.com/auth/facebook

Edit 2: My .env file:

'facebook' => [ FB_CLIENT_ID => '###', FB_SECRET_ID => 'this-is-secret!', ],


Solution

  • I think your two routes should not have the same URL, because Laravel has no way to know which to use.

    Facebook returns a ?code parameter, so make use of it! If ?code is included, then handle the login logic from Socialite, if not, redirect to Facebook.

    You have two unknown scopes in your code, scope1 and scope2, those are unknown to Facebook and will return an error when trying to access them. Use real scopes like public_profile or email and you should be set. (scopes are the permissions you are requesting)