First step:-
install laravel socialite package
composer require laravel/socialite Second step:- we need to configure the aliases and provider in config/app.php
'providers' => [ LaravelSocialiteSocialiteServiceProvider::class, ], 'aliases' => [ 'Socialite' => LaravelSocialiteFacadesSocialite::class, ],
Third step:-
Lets go to https://developers.facebook.com/apps/ and create a new app. We have put an app name, email on this page for creating an app.
After create the facebook app, go to setting->advanced and set redirect url
Fourth Step:-
Set client id and client secret config/service.php file :
'facebook' => [ 'client_id' => 'xxxx', 'client_secret' => 'xxx', 'redirect' => 'https://www.example.com/callback/facebook', ],
Fifth Step:-
Go to app/routes/web.php file and create two below routes here
Route::get('/auth/redirect/{provider}', 'SocialController@redirect'); Route::get('/callback/{provider}', 'SocialController@callback'); Six step:- create controller go to app/controllers/SocialController.php and put the below code
<?php
namespace
AppHttpControllers;
use
IlluminateHttpRequest;
use
Validator,Redirect,Response,File;
use
Socialite;
use
AppUser;
class
SocialController
extends
Controller
{
public
function
redirect(
$provider
)
{
return
Socialite::driver(
$provider
)->redirect();
}
public
function
callback(
$provider
)
{
$getInfo
= Socialite::driver(
$provider
)->user();
$user
=
$this
->createUser(
$getInfo
,
$provider
);
auth()->login(
$user
);
return
redirect()->to(
'/home'
);
}
function
createUser(
$getInfo
,
$provider
){
$user
= User::where(
'provider_id'
,
$getInfo
->id)->first();
if
(!
$user
) {
$user
= User::create([
'name'
=>
$getInfo
->name,
'email'
=>
$getInfo
->email,
'provider'
=>
$provider
,
'provider_id'
=>
$getInfo
->id
]);
}
return
$user
;
}
}
go to Resources/Views/Auth/login.blade.php and add a facebook social login button
<
div
class
=
"form-group row mb-0"
>
<
div
class
=
"col-md-8 offset-md-4"
>
<
a
href
=
"{{ url('/auth/redirect/facebook') }}"
class
=
"btn btn-primary"
><
i
class
=
"fa fa-facebook"
></
i
> Facebook</
a
>
</
div
>
</
div
>
Demo