Using Laravel Socialite to Login to Facebook

Using Laravel Socialite to login to facebook

In your composer.json add- "laravel/socialite": "~2.0",

"require": {
"laravel/framework": "5.0.*",
"laravel/socialite": "~2.0",

the run composer update

In config/services.php add:

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

You need to 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 like so:

Sample Image

laravel Socialite facebook login

Hi you are missing to give credentials of social media put that in config/services.php

'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => env('CALLBACK_URL_FACEBOOK'),
]

And run the following commands :

php artisan clear-compiled 
composer dump-autoload
php artisan optimize


Related Topics



Leave a reply



Submit