1

My expected result is to have the user redirected to my homepage after facebook login. I am using Socialite, Laravel 5.4, and Xampp. After being able to login through facebook, my url is now in callback in which the callback is calling the logincontroller that redirects to the homepage. So problem is after redirected to homepage, my url has now some hashed value.

localhost/sampleProject/public/login/facebook/callback?code=AQBZpjdW...

and when I reload page it shows error invalid state exception in abstractprovider.php. Am I missing something in my functions?

Inside my login controller

    public function redirectToProvider()
    {
        return Socialite::driver('facebook')->redirect();
    }

    /**
     * Obtain the user information from facebook.
     *
     * @return Response
     */
    public function handleProviderCallback()
    {
        $user = Socialite::driver('facebook')->user();

        return view('user-profile', compact('user',$user));
    }

Inside my services.php file

'facebook' => [
    'client_id' => 'insert_app_id_here',
    'client_secret' => 'enter_app_secret_here',
    'redirect' => 'http://localhost/sampleProject/public/login/facebook/callback',
],

Inside my routes/web.php

Route::get('login/facebook', 'Auth\LoginController@redirectToProvider');
Route::get('login/facebook/callback', 'Auth\LoginController@handleProviderCallback');

I also have included these in my config/app.php

Laravel\Socialite\SocialiteServiceProvider::class,

and

'Socialite' => Laravel\Socialite\Facades\Socialite::class,
Ralph
  • 193
  • 1
  • 4
  • 12

2 Answers2

2

You could try this thing)

public function handleProviderCallback(Request $request)
{
    session()->put('state', $request->input('state'));
    $user = Socialite::driver('facebook')->user();

    return view('user-profile', compact('user',$user));
}
-2

First of all, run the following command:

composer require laravel/socialite

After that In app/config.php add the following line in providers.

Laravel\Socialite\SocialiteServiceProvider::class,

After that In app/config.php add the following line in aliases

'Socialite' => Laravel\Socialite\Facades\Socialite::class,

In config/services.php add:

//Socialite
    'facebook' => [
        'client_id'     => '1234567890444',
        'client_secret' => '1aa2af333336fffvvvffffvff',
        'redirect'      => 'http://laravel.dev/login/callback/facebook',
    ],

Now create two routes, mine are like these:

//Social Login
Route::get('/login/{provider?}',[
    'uses' => 'AuthController@getSocialAuth',
    'as'   => 'auth.getSocialAuth'
]);

Route::get('/login/callback/{provider?}',[
    'uses' => 'AuthController@getSocialAuthCallback',
    'as'   => 'auth.getSocialAuthCallback'
]);

You also need to create controller for the routes above like so:

<?php namespace App\Http\Controllers;

 use Laravel\Socialite\Contracts\Factory as Socialite;

 class AuthController extends Controller
 {

       public function __construct(Socialite $socialite){
           $this->socialite = $socialite;
       }


       public function getSocialAuth($provider=null)
       {
           if(!config("services.$provider")) abort('404'); //just to handle providers that doesn't exist

           return $this->socialite->with($provider)->redirect();
       }


       public function getSocialAuthCallback($provider=null)
       {
          if($user = $this->socialite->with($provider)->user()){
             dd($user);
          }else{
             return 'something went wrong';
          }
       }

 }

and finally, add Site URL to your Facebook App. I followed this article for the proposed solution above. https://www.cloudways.com/blog/social-login-in-laravel-using-socialite/

Saquib Lari
  • 190
  • 8