Socialite authentication in Laravel

Last Updated: December 10, 2017

First thing you have to do is to install socialite in your Laravel project
You can use the following command in terminal

composer require laravel/socialite

In config\app.php file you need to add following codes for the configurations
under the providers
Laravel\Socialite\SocialiteServiceProvider::class,
under the Alias
'Socialite' => Laravel\Socialite\Facades\Socialite::class

You need to add following two functions for the App\Http\Controllers\Auth\LoginController.php file

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

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

        // $user->token;
    }

In config\service.php file you can add setting for Facebook as shown in the below code

[...]
   'facebook' => [
        'client_id' => '1955539357796413',
        'client_secret' => 'c74edf8a39b2d6c782f8e725ba7d5f91',
        'redirect' => 'http://localhost/public/login/facebook/callback',
    ],
[...]

Next, you can add following code to your routes\web.php file

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

Now you can test the code with URL http://localhost/public/login/facebook/

Related Errors

You may get the following errors when you test with the localhost

(1/1) RequestException
cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

To solve this problem you can download the https://curl.haxx.se/ca/cacert.pem and you can copy the files to /Applications/XAMPP/xamppfiles
Then you can update the php.ini file in your server with following entry
curl.cainfo = “/Applications/XAMPP/xamppfiles/cacert.pem”